Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move wrapper load_qasm_* methods to a submodule #533

Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
@@ -7,14 +7,12 @@

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

import os
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
@@ -217,12 +215,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,
@@ -241,12 +234,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)