Interfaces¶
Interfaces to external packages all operate under the same overall design strategy.
They each define a get_config()
function which returns a
Config
object containing all gates that are compatible with the
interface gate definitions.
However, the default Config objects typically have multiple
NativeGate
s that implement the same Gate
for
different parameters. For example Identity
is a special case of both Z(phi)
and X(phi)
when phi=0
. Each interface fixes this degeneracy by maintaining
a “priority list” containing the names of the gates found in the external package.
The priority list for an interface can be accessed and altered using the corresponding
methods get_priority_list()
and set_priority_list()
.
When an interface is successfully loaded, Circuit
gets additional
functionality in the form of a Circuit.to_<interface>()
, which will convert
the True-Q™ circuit representation into the representation of the external package. By
altering the priority list for the specified package, the user can select specific gates
to build into by default. Additional advanced options can be accessed by passing custom
config objects which are compatable with the interface gates, see the corresponding
from_trueq_circ()
for more information.
-
class
trueq.interface.metadata.
Metadata
(mapping=None)¶ Stores data not recorded in
Circuit
objects to enable lossless conversion.- Parameters
mapping (
dict
) – A dictionary mapping True-Q™ qubit labels to the indexing of the target framework.
Cirq¶
-
trueq.interface.cirq.
from_trueq_circ
(tq_circ, metadata=None, config='ZXZ', device=None, join_meas=True)¶ Converts a
Circuit
into acirq.Circuit
.import cirq import trueq as tq circ = tq.Circuit([{(0, 1): tq.Gate.cx}]) ext_circ = tq.interface.cirq.from_trueq_circ(circ) ext_circ.to_trueq().draw()
Note
Arbitrary single-qubit operations are decomposed using the
ZXZ
QubitMode
by default.If an SU(4) gate is provided which is not directly equivalent to a single gate in
get_priority_list()
, it is decomposed using theCompiler()
.- Parameters
circuit (
Circuit
) – The True-Q™ circuit to be converted into a Cirq circuit.metadata (
CirqMetadata
) – Metadata required to accurately reproduce the original Cirq circuit. If this is not provided, measurements are added at the end of the circuit.config (
Config
|str
) – If aConfig
which contains gates which are directly decomposable into Cirq native gates is provided, then conversion will take place into gates specified by that config. Alternatively,config
accepts text strings and uses them as themode
argument forget_config
.device (
None
orcirq.Device
) – Acirq.Device
, if no device is provided, an unconstrained device withcirq.GridQubit
is assumed.join_meas (
bool
) – This determines ifMeas
are joined into singlecirq.meas()
objects, or single meas object in parallel in the given moment/cycle.
- Return type
cirq.Circuit
-
trueq.interface.cirq.
from_trueq_gate
(gate)¶ Converts a
Gate
into acirq.ops.GateOperation
.When multiple Cirq gates can represent the True-Q™ gate, a priority list (accessible via
get_priority_list()
and customizable viaset_priority_list()
) is used to choose which Cirq gate is used.- Parameters
tq_gate (
Gate
) – The True-Q™ gate to be converted into a Cirq gate.- Raises
CirqConversionError – If conversion is not possible.
-
trueq.interface.cirq.
get_config
(mode='ZXZXZ')¶ Gets a
Config
containing all compatible Cirq gates listed in the priority list for the Cirq interface, seeget_priority_list()
.
-
trueq.interface.cirq.
get_priority_list
()¶ Returns the current conversion priority list specifying the order in which
cirq.ops.GateOperation
s should be used byfrom_trueq_gate()
to try to construct a Cirq gate.The priority list is used by
get_config()
to build aConfig
object.The priority list can be altered using
set_priority_list()
.- Return type
list
-
trueq.interface.cirq.
set_priority_list
(priority)¶ Set the priority list and reset all caching for the Cirq interface.
See
get_priority_list()
for a full description.- Parameters
priority (
list
) – The priority list. IfNone
is provided, the priority list is reset to the default.
-
trueq.interface.cirq.
to_trueq_circ
(cirq_circ)¶ Converts a
cirq.Circuit
into aCircuit
.This returns a tuple of the True-Q™ circuit and a
CirqMetadata
which contains information present in the Cirq circuit which is not represented in True-Q™ circuits and is required for the reverse conversion (seefrom_trueq_circ()
).Note
Measurements are always placed at the end of the circuit.
Currently only supports Cirq circuits using GridQubits.
- Parameters
cirq_circ – A Cirq representation of a circuit.
- Type
cirq.Circuit
- Return type
tuple
of (Circuit
,CirqMetadata
)
-
trueq.interface.cirq.
to_trueq_gate
(ext_gate)¶ Converts a
cirq.ops.GateOperation
into aGate
.- Parameters
ext_gate (
cirq.ops.GateOperation
) – The Cirq gate to be converted into a True-Q™ gate.- Return type
- Raises
CirqConversionError – If conversion is not possible
-
class
trueq.interface.metadata.
CirqMetadata
(mapping=None, measure_mapping=None)¶ Stores data not recorded in
Circuit
objects to enable lossless conversion.Note
Measurements recorded in
cirq.Circuit
objects are always assumed to be placed at the end of the circuit.If no measurements are provided through
measure_mapping
, then Measurements are automatically added on all qubits- Parameters
mapping (
dict
) – A dictionary mapping True-Q™ qubit labels tocirq.Device
s.measure_mapping (
dict
) – A dictionary mapping True-Q™ labels to the name of a measurement name as defined bycirq.measure()
-
class
CirqMeasure
(tq_qubits, meas_name, invert)¶ -
property
invert
¶ Alias for field number 2
-
property
meas_name
¶ Alias for field number 1
-
property
tq_qubits
¶ Alias for field number 0
-
property
-
measure
(tq_qubits, meas_name=None, invert=())¶ Add a record of a measurement on a given qubit.
This assumes that all measurements happen at the end of the circuit.
- Parameters
tq_qubits (list[tuple]) – The True-Q™ label for the qubits.
meas_name (str) – The name of the measurement in Cirq.
invert (Iterable[bool]) – bitmask of if the output of the measurement is expected to be inverted, see the Cirq documentation on measurements for more information.
PyQuil¶
-
trueq.interface.pyquil.
from_trueq_circ
(circ, metadata=None, config='ZXZ')¶ Converts a
Circuit
into apyquil.Program
.import pyquil import trueq as tq circ = tq.Circuit([{(0, 1): tq.Gate.cx}]) ext_circ = tq.interface.pyquil.from_trueq_circ(circ) ext_circ.to_trueq().draw()
0 1 Key: Labels: (0, 1) Name: CNOT() Aliases: Gate.cx Gate.cnot Parameters: Generators: ZI: 90.00 ZX: -90.00 IX: 90.00 1.00 1.00 1.00 1.00 CX CX Note
Measurements are always assumed to be at the end of a circuit.
Arbitrary single-qubit operations are decomposed using the
ZXZ
QubitMode
by default.If an SU(4) gate is provided which is not directly equivalent to a single gate in
get_priority_list()
, it is decomposed using theCompiler()
.- Parameters
circ (
Circuit
) – The True-Q™ circuit to be converted into a PyQuil Program.metadata (
PyquilMetadata
) – Metadata required to accurately reproduce the original PyQuil program. By default, measurements are placed at the end of the circuit on all qubits, reading out to a classical register called ‘ro’.config (
Config
|str
) – If aConfig
which contains gates which are directly decomposable into PyQuil native gates is provided, then conversion will take place into gates specified by that config. Alternatively,config
accepts text strings and uses them as themode
argument forget_config
.
- Return type
pyquil.Program
-
trueq.interface.pyquil.
from_trueq_gate
(gate)¶ Converts a
Gate
into apyquil.quilbase.Gate
.When multiple PyQuil gates can represent the True-Q™ gate, a priority list (accessible via
get_priority_list()
and customizable viaset_priority_list()
) is used to choose which PyQuil gate is used.- Parameters
tq_gate (
Gate
) – The True-Q™ Gate to be converted into a PyQuil gate.- Raises
PyquilConversionError – If conversion is not possible.
-
trueq.interface.pyquil.
get_config
(mode='ZXZXZ')¶ Gets a
Config
containing all compatible PyQuil gates listed in the priority list for the PyQuil interface, seeget_priority_list()
.
-
trueq.interface.pyquil.
get_priority_list
()¶ Returns the current conversion priority list specifying the order in which
pyquil.quilbase.Gate
s should be used byfrom_trueq_gate()
to try to construct a PyQuil gate.The priority list is used by
get_config()
to build aConfig
object.The priority list can be altered using
set_priority_list()
.- Return type
list
-
trueq.interface.pyquil.
set_priority_list
(priority)¶ Set the priority list and reset all caching for the PyQuil interface.
See
get_priority_list()
for a full description.- Parameters
priority (
list
) – The priority list. IfNone
is provided, the priority list is reset to the default.
-
trueq.interface.pyquil.
to_trueq_circ
(program)¶ Converts a
pyquil.Program
into aCircuit
.This returns a tuple of the True-Q™ circuit and a
PyquilMetadata
which contains information present in the PyQuil program which is not represented in True-Q™ circuits and is required for the reverse conversion (seefrom_trueq_circ()
).- Parameters
program (
pyquil.Program
) – The pyquil program to be converted into a True-Q™ circuit.- Return type
tuple
of (Circuit
,PyquilMetadata
)
-
trueq.interface.pyquil.
to_trueq_gate
(ext_gate)¶ Converts a
pyquil.quilbase.Gate
into aGate
.This only works for gates defined in
pyquil.gate_matrices.QUANTUM_GATES
.- Parameters
ext_gate (
pyquil.quilbase.Gate
) – The PyQuil gate to be converted into a True-Q™ gate.- Return type
- Raises
PyquilConversionError – If conversion is not possible.
-
class
trueq.interface.metadata.
PyquilMetadata
(mapping=None, classical_regs=None, measure_mapping=None)¶ Stores data not recorded in
Circuit
objects to enable lossless conversion.Note
Measurements recorded in
pyquil.Program
objects are always assumed to be placed at the end of the circuit.- Parameters
mapping (
dict
) – A dictionary mapping True-Q™ qubit labels topyquil.device.Qubit
s.classical_regs (
dict
ofpyquil.quilbase.Declare
s) – A dictionary containing all of thepyquil.quilbase.Declare
s, where the keys are the names of the classical registers.measure_mapping (
dict
) – A dictionary mapping True-Q™ labels to the name of a measurement name as defined bypyquil.quilbase.Measurement()
Qiskit¶
-
trueq.interface.qiskit.
config_from_backend
(backend)¶ Takes an IBM Backend and converts it into a
Config
object.The resulting config will contain the chip topology constraints as well as the gates themselves.
import qiskit as qk from trueq.interface import qiskit as tqk tqk.config_from_backend(qk.providers.aer.QasmSimulator()) # For backends provided by the IBM Quantum Experience, some additional # authentication code is needed # qk.IBMQ.load_account() # provider = qk.IBMQ.get_provider() # backend = provider.get_backend("ibmqx2") # config = tq.interface.qiskit.config_from_backend(backend)
Mode: ZXZXZ Dimension: 2 Gate u1: Hamiltonian: - [Z, lam * 180 / pi] Involving: {} Gate u2: Hamiltonian: - [Z, phi * 180 / pi] - [Y, '90'] - [Z, lam * 180 / pi] Involving: {} Gate u3: Hamiltonian: - [Z, phi * 180 / pi] - [Y, theta * 180 / pi] - [Z, lam * 180 / pi] Involving: {} Gate cx: Matrix: - [(1+0j), 0j, 0j, 0j] - [0j, (1+0j), 0j, 0j] - [0j, 0j, 0j, (1+0j)] - [0j, 0j, (1+0j), 0j] Involving: {} Gate cy: Matrix: - [(1+0j), 0j, 0j, 0j] - [0j, (1+0j), 0j, 0j] - [0j, 0j, 0j, (-0-1j)] - [0j, 0j, 1j, 0j] Involving: {} Gate cz: Matrix: - [(1+0j), 0j, 0j, 0j] - [0j, (1+0j), 0j, 0j] - [0j, 0j, (1+0j), 0j] - [0j, 0j, 0j, (-1+0j)] Involving: {} Gate id: Matrix: - [(1+0j), 0j] - [0j, (1+0j)] Involving: {} Gate x: Matrix: - [0j, (1+0j)] - [(1+0j), 0j] Involving: {} Gate y: Matrix: - [0j, (-0-1j)] - [1j, 0j] Involving: {} Gate z: Matrix: - [(1+0j), 0j] - [0j, (-1+0j)] Involving: {} Gate h: Matrix: - [(0.7071067811865476+0j), (0.7071067811865476+0j)] - [(0.7071067811865476+0j), (-0.7071067811865476+0j)] Involving: {} Gate s: Matrix: - [(0.7071067811865476-0.7071067811865475j), 0j] - [0j, (0.7071067811865476+0.7071067811865475j)] Involving: {} Gate sx: Matrix: - [(0.7071067811865476+0j), -0.7071067811865475j] - [-0.7071067811865475j, (0.7071067811865476+0j)] Involving: {} Gate t: Matrix: - [(0.9238795325112867-0.3826834323650898j), 0j] - [0j, (0.9238795325112867+0.3826834323650898j)] Involving: {} Gate sdg: Matrix: - [(0.7071067811865476+0.7071067811865475j), -0j] - [-0j, (0.7071067811865476-0.7071067811865475j)] Involving: {} Gate tdg: Matrix: - [(0.9238795325112867+0.3826834323650898j), -0j] - [-0j, (0.9238795325112867-0.3826834323650898j)] Involving: {} Gate rx: Hamiltonian: - [X, theta] Involving: {} Gate ry: Hamiltonian: - [Y, theta] Involving: {} Gate rz: Hamiltonian: - [Z, theta] Involving: {}
- Parameters
backend (
qiskit.provider.BaseBackend
) – A backend from qiskit.- Returns
A config which contains gates and topology of a given backend.
- Return type
-
class
trueq.interface.qiskit.
Executor
(circuits, backend, filename=None, n_shots=128, max_submissions=5, overwrite=False)¶ A class to asynchronously submit a
CircuitCollection
to aqiskit.provider.BaseBackend
.Since current hardware devices have a limit to the number of circuits that can be run at any one time, batching of circuits must be performed. An example of this is the ibmqx2 chip provided by IBM, it has a stated limit of 75 circuits which may be submitted to it at any one time. This means that if a user wishes to submit more than 75 circuits, the total collection of circuits needs to be broken up into groups (or batches) of no more than 75 circuits. True-Q™ circuit collections have native support for batching circuits like this, see
batch()
.The
Executor
converts batched circuits into Qiskit formatted objects, and submits them to the provided backend as jobs on the physical devices. The executor then keeps track of the status of each job asynchronously, updating the status periodically as to where the jobs are in the device’s queue.Since most devices have a limit as to the number of batches that can be submitted by one user at any given time, the executor will only submit up to a
max_submissions
number of batches at any one time. Once a batch has run on the device, and has been removed from the job queue, the executor will place another batch into the queue until there are a total ofmax_submissions
submitted jobs at any one time.Here is a worked example, where a simple circuit is submitted to the IBMQ simulator backend:
import trueq as tq import qiskit as qk # loading IBMQ account and selecting a backend # qk.IBMQ.load_account() # provider = qk.IBMQ.get_provider() # backend = provider.get_backend('ibmq_qasm_simulator') backend = qk.providers.aer.QasmSimulator() # Generating a simple circuit of only the H gate circs = tq.Circuit([{0: tq.Gate.h}, {0: tq.Meas()}]) # The handling of the submission of the circuits ex = tq.interface.qiskit.Executor(circs, backend) # This is a blocking operation, and will block until all data has been acquired ex.results().results # A more complicated example using advanced batching options and saving # the results circs2 = tq.make_srb((0, 1), [4, 64, 128]) # Batch the circuits into groups of 75, including readout calibration circuits # in each batch ro_circuits = tq.make_rcal([0, 1]) batches = circs2.batch(75, extra_circuits=ro_circuits) ex2 = tq.interface.qiskit.Executor(batches, backend) ex2.results().results
- Parameters
circuits (
CircuitCollection
|Circuit
|generator
) –The circuits which are to be submitted to the Qiskit backend. Depending on what is submitted here, there are several possible outcomes:
Circuit
It will be converted to a
CircuitCollection
. Results will be placed back inside of the original circuit object itself, and can be viewed as data is retrieved from the backend without having to wait.
CircuitCollection
It will be batched using the maximum allowed batch size for the given backend. For very long circuits this may result in errors due to limited memory of the classical hardware which control the backend. If errors occur during the submission of a
CircuitCollection
, it is recommended to resubmit using the third submission type. Results are placed back inside of the original circuits as data is retrieved from the server.
batch
The circuits will be batched exactly as given by the batch generator. This can be used to submit automatic readout correction circuits, see above example or
batch()
for more details about possible options. Results are placed inside original circuits as data comes in, note that some batching options may result in additional circuits being added to the collection.
backend (
qiskit.provider.BaseBackend
) – The Qiskit backend to which the circuits will be submitted.filename (
str
) – Optional filename where results will be saved. If this is provided the Executor will also be able to partially recover from a crash of the python session, loss of internet, or other interruption in data acquisition.n_shots (
int
) – The number of shots of the circuits to be aquired.max_submissions (
int
) – The maximum number of batches allowed in a backend queue at any one time. This number is set by the backend provider and should be set here accordingly.overwrite (
bool
) – Whether to overwrite the file on disk if it exists before these circuits are executed. Note that regardless of the value of this flag, after this first check, the file on disk will continually be updated as new results come in.
-
property
n_shots
¶ Number of shots to be aquired by the backend.
- Return type
int
-
property
backend
¶ Qiskit backend where the circuits will be submitted.
- Return type
qiskit.provider.BaseBackend
-
property
filename
¶ Filename where circuits with results will be saved.
- Return type
str
-
property
circuits
¶ Batched
CircuitCollection
, if a single circuit was provided, it will have been converted into aCircuitCollection
. SeeExecutor
documentation for more details.- Return type
list
-
results
()¶ Returns the
CircuitCollection
complete with results.Note
This is a blocking operation, and once called you will have to wait until all circuits have completed acquisition on the backend.
This is not required to be called in order to get results, as results are automatically placed into the provided circuits when they are retrieved from the backend. This function is simply a means to force code to wait until the backend returns all of the results.
- Return type
-
property
status
¶ Returns a text representation for the current status of all jobs.
This is a string equivalent of the html output provided in jupyter.
- Return type
str
-
save
(overwrite=False)¶ Saves the current
CircuitCollection
regardless of status of acquisition.This is called automatically whenever a new batch is either submitted or retrieved from the backend.
- Parameters
overwrite (
bool
) – Whether to force an overwrite of the existing file.
-
cancel
()¶ Cancels all currently submitted jobs if possible.
It is recommended that this be run any time a submission mistake is made, as circuits that have been submitted to the backend will stay in queue otherwise.
-
trueq.interface.qiskit.
from_trueq_circ
(circuit, metadata=None, config=None)¶ Converts a
Circuit
into aqiskit.QuantumCircuit
.import qiskit import trueq as tq circ = tq.Circuit([{(0, 1): tq.Gate.cx}]) ext_circ = tq.interface.qiskit.from_trueq_circ(circ) ext_circ.to_trueq().draw()
0 1 Key: Labels: (0, 1) Name: Gate.cx Aliases: Gate.cx Gate.cnot Locally Equivalent: CNOT Generators: ZI: 90.00 ZX: -90.00 IX: 90.00 1.00 1.00 1.00 1.00 CX CX Note
Single qubit gates are reduced as much as possible up to numerical precision, e.g., some
U3
gates becomeU2
orU1
gates.Blocking operations are added at the end of every
Cycle
to ensure that the physical noise process is consistent.- Parameters
circuit (
Circuit
) – The True-Q™ circuit to be converted into a Qiskit QuantumCircuitmetadata (
QiskitMetadata
) – Metadata required to accurately reproduce the original Qiskit QuantumCircuit. By default, the returned QuantumCircuit will have one quantum register and one classical register and have measurements at the end of the circuit mapping each quantum bit to each classical bit.config (
Config
) – A given config object which contains gates which can be performed by qiskit, but also has hardware restrictions. For example, a Config object can represent a CNOT gate that is restricted to specific sets of qubits, and a CNOT gate is a known native gate in Qiskit. Providing this config means that not only will the circuit be converted into Qiskit representation, but also that it will satisfy hardware restrictions. If no config is provided, all possible qiskit gates are used in the compilation.
- Returns
A Qiskit circuit representation of the
Circuit
- Return type
qiskit.QuantumCircuit
-
trueq.interface.qiskit.
from_trueq_gate
(gate)¶ Converts a
Gate
into aqiskit.circuit.Gate
.When multiple Qiskit gates can represent the True-Q™ gate, a priority list (accessible via
get_priority_list()
and customizable viaset_priority_list()
) is used to choose which Qiskit gate is used.- Parameters
tq_gate (
Gate
) – The True-Q™ Gate to be converted into a Qiskit gate.- Raises
QiskitConversionError – If conversion is not possible.
-
trueq.interface.qiskit.
get_config
(mode='ZXZXZ')¶ Get a
Config
containing all compatible Qiskit gates listed in the priority list for the Qiskit interface, seeget_priority_list()
.
-
trueq.interface.qiskit.
get_priority_list
()¶ Returns the current conversion priority list specifying the order in which
qiskit.circuit.Gate
s should be used byfrom_trueq_gate()
to try to construct a Qiskit gate.The priority list is used by
get_config()
to build aConfig
object.The priority list can be altered using
set_priority_list()
.- Return type
list
-
trueq.interface.qiskit.
randomly_compile_qiskit
(circuit, twirl=None)¶ Randomly compiles a given Qiskit QuantumCircuit or DAGCircuit object.
Accepts the same options as
randomly_compile()
-
class
trueq.interface.qiskit.
RCPass
(*args, **kwargs)¶ This is a Pass object as used in Qiskit’s transpiler object. This enables use of True-Q™’s randomized compilation tools in the framework of Qiskit.
Randomly compile a given DAG.
Beginner users are recommended to use the defaults.
-
run
(dag)¶ Randomly Compile a
DAGCircuit
- Return type
DAGCircuit
-
-
trueq.interface.qiskit.
set_priority_list
(priority)¶ Set the priority list and reset all caching for the Qiskit interface.
See
get_priority_list()
for a full description.- Parameters
priority (
list
) – The priority list. IfNone
is provided, the priority list is reset to the default.
-
trueq.interface.qiskit.
to_trueq_circ
(qk_circ)¶ Converts a
qiskit.QuantumCircuit
into aCircuit
.This returns a tuple of the True-Q™ circuit and a
QiskitMetadata
which contains information present in the Qiskit circuit which is not represented in True-Q™ circuits and is required for the reverse conversion (seefrom_trueq_circ()
).Note
Barriers are not preserved, and measurements are always placed at the end of the circuits.
- Parameters
dag (
qiskit.QuantumCircuit
) – A qiskit circuit representation of a circuit.- Return type
tuple
of (Circuit
,QiskitMetadata
)
-
trueq.interface.qiskit.
to_trueq_gate
(ext_gate)¶ Converts a
qiskit.circuit.Gate
into aGate
.- Parameters
ext_gate (
qiskit.circuit.Gate
) – The Qiskit gate to be converted into a True-Q™ gate.- Return type
- Raises
QiskitConversionError – If conversion is not possible.
-
class
trueq.interface.metadata.
QiskitMetadata
(mapping=None, meas_pairs=None)¶ Stores data not recorded in
Circuit
objects to enable lossless conversion.- Parameters
mapping (
dict
) – A dictionary mapping True-Q™ qubit labels toqiskit.Qubit
s.meas_pairs (
list
) – A list of pairs, each with type (qiskit.Qubit
,qiskit.Bit
), whose order matches that ofmeas_locs
. This stores which classical register each quantum measurement is to be stored into.
QASM¶
-
class
trueq.interface.qasm.
QASM
¶ Interface class to convert
Circuit
s into the QASM 2.0 language.This is the only interface which does not use a priority list.
QASM only defines two possible gate operations natively:
U
This is an arbitrary single qubit unitary with three free parameters. A
GateFactory
definition for this can be found intrueq.config.factory.u3_factory
.
CX
This is a fixed two qubit gate, more commonly called
CNOT
.
Any other gates used in a QASM file must be defined as a subroutine using these two fundamental gates.
Note
A
Config
containing only these two gates is defined inQASM_CONFIG()
.The
QASM
conversion class stores aConfig
which contains any one or two qubit fixed gates, as well as any of the following parameterized single qubit operations:Pauli rotation on a single qubit.
u1_factory
or exact mathematical equivalent.u2_factory
or exact mathematical equivalent.u3_factory
or exact mathematical equivalent.
This config can be altered via
set_config()
andget_config()
, and it will automatically be converted to gate definitions inside of the QASM header and may be used inside of the QASM file itself. If no config is provided, then it is assumed that theqelib1
definition file is available, and this file is included as an import in the output QASM. A config containing the definitions for this library can be accessed throughQELIB1()
.import trueq as tq circ = tq.make_xrb([0], [5], 1)[0] print(circ.to_qasm())
// True-Q Version: 2.8.5 // Circuit Key: // {'measurement_basis': 'Y', 'n_random_cycles': 5, 'protocol': 'XRB', 'seq_label': 2206, 'twirl': Twirl({(0,): 'C'})} OPENQASM 2.0; include "qelib1.inc"; qreg q[1]; creg c[1]; u2(-3.141592653589793,0.0) q[0]; barrier q; u2(-4.71238898038469,3.141592653589793) q[0]; barrier q; u2(-1.5707963267948966,3.141592653589793) q[0]; barrier q; u2(-4.71238898038469,0.0) q[0]; barrier q; u1(-1.5707963267948968) q[0]; barrier q; measure q[0] -> c[0];
Building into custom defined QASM gates:
import trueq as tq zz_fact = tq.config.GateFactory("ZZ", matrix=tq.Gate.from_generators("ZZ", 90)) factories = [tq.interface.QASM.QELIB1().u1, tq.interface.QASM.QELIB1().u2, tq.interface.QASM.QELIB1().u3, zz_fact] config = tq.Config(factories=factories) tq.interface.QASM.set_config(config) circ = tq.make_xrb([0], [5], 1)[0] print(circ.to_qasm())
// True-Q Version: 2.8.5 // Circuit Key: // {'measurement_basis': 'Y', 'n_random_cycles': 5, 'protocol': 'XRB', 'seq_label': 2297, 'twirl': Twirl({(0,): 'C'})} OPENQASM 2.0; gate u1(theta) q { U(0,0,lambda) q; } gate u2(phi,lambda) q { U(pi/2,phi,lambda) q; } gate u3(theta,phi,lambda) q { U(theta,phi,lambda) q; } gate ZZ q0, q1 { U(0.0,-4.71238898038469,1.5707963267948966) q0; U(1.5707963267948966,-3.141592653589793,0.0) q1; CX q0,q1; U(0.0,0.0,1.5707963267948966) q0; U(1.5707963267948966,-1.5707963267948966,0.0) q1; } qreg q[1]; creg c[1]; u3(3.141592653589793,-3.141592653589793,1.5707963267948966) q[0]; barrier q; u2(-1.5707963267948966,1.5707963267948966) q[0]; barrier q; u2(-3.141592653589793,0.0) q[0]; barrier q; u1(-1.5707963267948968) q[0]; barrier q; u3(3.141592653589793,-3.141592653589793,1.5707963267948966) q[0]; barrier q; measure q[0] -> c[0];
-
classmethod
get_config
()¶ The
Config
used to build the QASM representation.This config will automatically be converted to gate definitions inside of the QASM header and may be used inside of the QASM file itself. See
from_trueq_circ()
for details on output.- Type
-
classmethod
set_config
(config=None)¶ Set the
Config
which contains any one or two qubit fixed gates, as well as any of the following parameterized single qubit operations:Pauli rotation on a single qubit.
u1_factory
or exact mathematical equivalent.u2_factory
or exact mathematical equivalent.u3_factory
or exact mathematical equivalent.
The provided config will automatically be converted to QASM compatible gate definitions inside of the QASM header and may be used inside of the QASM file itself. If no config is provided, then it is assumed that the
qelib1
definition file is available, and this file is included as an import in the output QASM. A config containing the definitions for this library can be accessed throughQELIB1()
.- Parameters
config (
Config
) – The config containing gate definitions that are desired for the QASM representation.
-
classmethod
get_header
()¶ Return the lines of the header currently associated with the currently desired QASM representation as defined by the
get_config()
.- Return type
str
-
classmethod
from_trueq_gate
(gate)¶ Convert a
Gate
into a QASM gate in the currently specified QASM respresentation as defined byget_config()
.- Return type
str
- Raises
ValueError – If conversion is not possible.
-
classmethod
from_trueq_circ
(circuit, custom_header=None, include_auto_header=True)¶ Converts a
Circuit
into a QASM 2.0 text string.Circuits converted to QASM will use the gate definitions found in the
get_config()
. These definitions will by default be included at the top of the output QASM file (this behavior can be disabled by settinginclude_auto_header=False
). Additional lines in the header can be included by providing a list of strings tocustom_header
, which will be appended as new lines at the top of the file.- Parameters
circuit (
Circuit
) – The circuit to be converted to QASM.custom_header (
list
) – A list of strings which will be put at the top of the file, with each string being placed on a seperate line.include_auto_header (
bool
) – If theget_config
is not the qelib1 config, then QASM requires all gates present to be written as combinations ofU3
andCNOT
gates (see the docs forQASM
, for more details), this flag determines if these definitions should be included in the header of the QASM file.
- Return type
str
-
classmethod
QASM_CONFIG
()¶ A
Config
containing the two possible gate operations defined by QASM natively:U
This is an arbitrary single qubit unitary with three free parameters. A
GateFactory
definition for this can be found intrueq.config.factory.u3_factory
.
CX
This is a fixed two qubit gate, more commonly called
CNOT
.
Any other gates used in a QASM file must be defined by these two fundamental gates.
- Return type
-
classmethod
QELIB1
()¶ A
Config
which contains the one and fixed two qubit gates found in the qelib1 library commonly used in qasm representations.See: https://github.com/Qiskit/openqasm/blob/master/examples/generic/qelib1.inc
This config is the default used by the QASM converter unless another is specified.
- Return type