Skip to content

Commit

Permalink
Move wrapper load_qasm_* methods to a submodule (#533)
Browse files Browse the repository at this point in the history
  • Loading branch information
delapuente authored and atilag committed Jun 11, 2018
1 parent 00d37c7 commit 80c5de1
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 18 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Changed
(#531).
- Renamed the specification schemas (#464).
- Convert ``LocalJob`` tests into unit-tests. (#526)
- Move wrapper ``load_qasm_*`` methods to a submodule (#533).

Removed
-------
Expand Down
73 changes: 73 additions & 0 deletions qiskit/wrapper/_circuittoolkit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# -*- coding: utf-8 -*-

# Copyright 2018, IBM.
#
# This source code is licensed under the Apache License, Version 2.0 found in
# the LICENSE.txt file in the root directory of this source tree.

"""
Utilities regarding the creation of QuantumCircuits from a variety of different
file formats.
"""

import os
from qiskit import QISKitError
from qiskit.qasm import Qasm
from qiskit.unroll import Unroller, CircuitBackend


def circuit_from_qasm_string(qasm_string, name=None,
basis_gates="id,u0,u1,u2,u3,x,y,z,h,s,sdg,t,tdg,"
"rx,ry,rz,cx,cy,cz,ch,crz,cu1,cu3,swap,ccx,"
"cswap"):

"""Construct a quantum circuit from a qasm representation (string).
Args:
qasm_string (str): a string of qasm, or a filename containing qasm.
basis_gates (str): basis gates for the quantum circuit.
name (str or None): the name of the quantum circuit after loading
qasm text into it. If no name given, assign automatically.
Returns:
QuantumCircuit: circuit constructed from qasm.
Raises:
QISKitError: if the string is not valid QASM
"""

node_circuit = Qasm(data=qasm_string).parse()
unrolled_circuit = Unroller(
node_circuit, CircuitBackend(basis_gates.split(",")))
circuit_unrolled = unrolled_circuit.execute()
if name:
circuit_unrolled.name = name
return circuit_unrolled


def circuit_from_qasm_file(qasm_file, name=None,
basis_gates="id,u0,u1,u2,u3,x,y,z,h,s,sdg,t,tdg,rx,"
"ry,rz,cx,cy,cz,ch,crz,cu1,cu3,swap,ccx,"
"cswap"):

"""Construct a quantum circuit from a qasm representation (file).
Args:
qasm_file (str): a string for the filename including its location.
name (str or None): the name of the quantum circuit after
loading qasm text into it. If no name is give the name is of
the text file.
basis_gates (str): basis gates for the quantum circuit.
Returns:
QuantumCircuit: circuit constructed from qasm.
Raises:
QISKitError: if the file cannot be read.
"""
if not os.path.exists(qasm_file):
raise QISKitError('qasm file "{0}" not found'.format(qasm_file))
if not name:
name = os.path.splitext(os.path.basename(qasm_file))[0]

with open(qasm_file) as file:
qasm_data = file.read()

return circuit_from_qasm_string(
qasm_data, name=name, basis_gates=basis_gates)
21 changes: 3 additions & 18 deletions qiskit/wrapper/_wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@

"""Helper module for simplified QISKit usage."""

import os
import warnings
import qiskit._compiler
from qiskit import QISKitError
from qiskit.backends.ibmq.ibmqprovider import IBMQProvider
from qiskit.wrapper.defaultqiskitprovider import DefaultQISKitProvider
from qiskit import QuantumJob
from qiskit.qasm import Qasm
from qiskit.unroll import Unroller, CircuitBackend
from ._circuittoolkit import circuit_from_qasm_file, circuit_from_qasm_string


# Default provider used by the rest of the functions on this module. Please
Expand Down Expand Up @@ -232,12 +230,7 @@ def load_qasm_string(qasm_string, name=None,
Raises:
QISKitError: if the string is not valid QASM
"""
node_circuit = Qasm(data=qasm_string).parse()
unrolled_circuit = Unroller(node_circuit, CircuitBackend(basis_gates.split(",")))
circuit_unrolled = unrolled_circuit.execute()
if name:
circuit_unrolled.name = name
return circuit_unrolled
return circuit_from_qasm_string(qasm_string, name, basis_gates)


def load_qasm_file(qasm_file, name=None,
Expand All @@ -256,12 +249,4 @@ def load_qasm_file(qasm_file, name=None,
Raises:
QISKitError: if the file cannot be read.
"""
if not os.path.exists(qasm_file):
raise QISKitError('qasm file "{0}" not found'.format(qasm_file))
if not name:
name = os.path.splitext(os.path.basename(qasm_file))[0]

with open(qasm_file) as file:
qasm_data = file.read()

return load_qasm_string(qasm_data, name=name, basis_gates=basis_gates)
return circuit_from_qasm_file(qasm_file, name, basis_gates)

0 comments on commit 80c5de1

Please sign in to comment.