mqt.core

MQT Core - The Backbone of the Munich Quantum Toolkit.

Subpackages

Submodules

Package Contents

class Permutation(permutation: dict[int, int] | None = None)

Bases: collections.abc.MutableMapping[int, int]

A class to represent a permutation of the qubits in a quantum circuit.

Parameters:

permutation – The permutation to initialize the object with.

__getitem__(idx: int) int

Get the value of the permutation at the given index.

Parameters:

idx – The index to get the value of the permutation at.

Returns:

The value of the permutation at the given index.

__setitem__(idx: int, val: int) None

Set the value of the permutation at the given index.

Parameters:
  • idx – The index to set the value of the permutation at.

  • val – The value to set the permutation at the given index to.

__delitem__(key: int) None

Delete the value of the permutation at the given index.

Parameters:

key – The index to delete the value of the permutation at.

__iter__() Iterator[int]

Return an iterator over the indices of the permutation.

__len__() int

Return the number of indices in the permutation.

__eq__(other: object) bool

Check if the permutation is equal to another permutation.

__ne__(other: object) bool

Check if the permutation is not equal to another permutation.

__hash__() int

Return the hash of the permutation.

apply(controls: set[Control]) set[Control]
apply(targets: list[int]) list[int]

Apply the permutation to a list of targets.

Parameters:

targets – The list of targets to apply the permutation to.

Returns:

The list of targets with the permutation applied.

class QuantumComputation
class QuantumComputation(nq: int, nc: int = 0)
class QuantumComputation(filename: str | PathLike[str])

Bases: collections.abc.MutableSequence[operations.Operation]

The main class for representing quantum computations within the MQT.

Acts as mutable sequence of Operation objects, which represent the individual operations in the quantum computation.

Parameters:
  • nq – The number of qubits in the quantum computation.

  • nc – The number of classical bits in the quantum computation.

  • filename – The filename of the file to load the quantum computation from. Supported formats are OpenQASM2, OpenQASM3, Real, GRCS, TFC, QC.

property num_qubits: int

The total number of qubits in the quantum computation.

property num_ancilla_qubits: int

The number of ancilla qubits in the quantum computation.

Note

Ancilla qubits are qubits that always start in a fixed state (usually \(|0\\rangle\)).

property num_garbage_qubits: int

The number of garbage qubits in the quantum computation.

Note

Garbage qubits are qubits whose final state is not relevant for the computation.

property num_measured_qubits: int

The number of qubits that are measured in the quantum computation.

Computed as \(|qubits| - |garbage|\).

property num_data_qubits: int

The number of data qubits in the quantum computation.

Computed as \(|qubits| - |ancilla|\).

property num_classical_bits: int

The number of classical bits in the quantum computation.

property num_ops: int

The number of operations in the quantum computation.

property ancillary: list[bool]

A list of booleans indicating whether each qubit is ancillary.

property garbage: list[bool]

A list of booleans indicating whether each qubit is garbage.

property variables: set[Variable]

The set of variables in the quantum computation.

name: str

The name of the quantum computation.

global_phase: float

The global phase of the quantum computation.

initial_layout: Permutation

The initial layout of the qubits in the quantum computation.

This is a permutation of the qubits in the quantum computation. It is mainly used to track the mapping of circuit qubits to device qubits during quantum circuit compilation. The keys are the device qubits (in which a compiled circuit is expressed in), and the values are the circuit qubits (in which the original quantum circuit is expressed in).

Any operations in the quantum circuit are expected to be expressed in terms of the keys of the initial layout.

Examples

  • If no initial layout is explicitly specified (which is the default), the initial layout is assumed to be the identity permutation.

  • Assume a three-qubit circuit has been compiled to a four qubit device and circuit qubit 0 is mapped to device qubit 1, circuit qubit 1 is mapped to device qubit 2, and circuit qubit 2 is mapped to device qubit 3. Then the initial layout is {1: 0, 2: 1, 3: 2}.

output_permutation: Permutation

The output permutation of the qubits in the quantum computation.

This is a permutation of the qubits in the quantum computation. It is mainly used to track where individual qubits end up at the end of the quantum computation, for example after a circuit has been compiled to a specific device and SWAP gates have been inserted, which permute the qubits. The keys are the qubits in the circuit and the values are the actual qubits being measured.

Similar to the initial layout, the keys in the output permutation are the qubits actually present in the circuit and the values are the qubits in the “original” circuit.

Examples

  • If no output permutation is explicitly specified and the circuit does not contain measurements at the end, the output permutation is assumed to be the identity permutation.

  • If the circuit contains measurements at the end, these measurements are used to infer the output permutation. Assume a three-qubit circuit has been compiled to a four qubit device and, at the end of the circuit, circuit qubit 0 is measured into classical bit 2, circuit qubit 1 is measured into classical bit 1, and circuit qubit 3 is measured into classical bit 0. Then the output permutation is {0: 2, 1: 1, 3: 0}.

static from_qasm(qasm: str) QuantumComputation

Create a QuantumComputation object from an OpenQASM string.

Parameters:

qasm – The OpenQASM string to create the QuantumComputation object from.

Returns:

The QuantumComputation object created from the OpenQASM string.

num_single_qubit_ops() int

Return the number of single-qubit operations in the quantum computation.

num_total_ops() int

Return the total number of operations in the quantum computation.

Recursively counts sub-operations (e.g., from CompoundOperation objects).

depth() int

Return the depth of the quantum computation.

invert() None

Invert the quantum computation (in-place) by inverting each operation and reversing the order of the operations.

to_operation() Operation

Convert the quantum computation to a single operation.

This gives ownership of the operations to the resulting operation, so the quantum computation will be empty after this operation.

When the quantum computation contains more than one operation, the resulting operation is a CompoundOperation.

Returns:

The operation representing the quantum computation.

__len__() int

Return the number of operations in the quantum computation.

__getitem__(idx: int) Operation
__getitem__(idx: slice) list[Operation]

Get a slice of operations from the quantum computation.

Note

This gives write access to the operations in the given slice.

Parameters:

idx – The slice of operations to get.

Returns:

The operations in the given slice.

__setitem__(idx: int, op: Operation) None
__setitem__(idx: slice, ops: Iterable[Operation]) None

Set the operations in the given slice.

Parameters:
  • idx – The slice of operations to set.

  • ops – The operations to set in the given slice.

__delitem__(idx: int) None
__delitem__(idx: slice) None

Delete the operations in the given slice.

Parameters:

idx – The slice of operations to delete.

insert(idx: int, op: Operation) None

Insert an operation at the given index.

Parameters:
  • idx – The index to insert the operation at.

  • op – The operation to insert.

append(op: Operation) None

Append an operation to the end of the quantum computation.

Parameters:

op – The operation to append.

reverse() None

Reverse the order of the operations in the quantum computation (in-place).

clear() None

Clear the quantum computation of all operations.

add_ancillary_register(n: int, name: str = 'q') None

Add an ancillary register to the quantum computation.

Parameters:
  • n – The number of qubits in the ancillary register.

  • name – The name of the ancillary register.

add_classical_register(n: int, name: str = 'c') None

Add a classical register to the quantum computation.

Parameters:
  • n – The number of bits in the classical register.

  • name – The name of the classical register.

add_qubit_register(n: int, name: str = 'anc') None

Add a qubit register to the quantum computation.

Parameters:
  • n – The number of qubits in the qubit register.

  • name – The name of the qubit register.

unify_quantum_registers(name: str = 'q') None

Unify all quantum registers in the quantum computation.

Parameters:

name – The name of the unified quantum register.

initialize_io_mapping() None

Initialize the I/O mapping of the quantum computation.

If no initial layout is explicitly specified, the initial layout is assumed to be the identity permutation. If the circuit contains measurements at the end, these measurements are used to infer the output permutation.

set_circuit_qubit_ancillary(q: int) None

Set a circuit (i.e., logical) qubit to be ancillary.

Parameters:

q – The index of the circuit qubit to set as ancillary.

set_circuit_qubits_ancillary(q_min: int, q_max: int) None

Set a range of circuit (i.e., logical) qubits to be ancillary.

Parameters:
  • q_min – The minimum index of the circuit qubits to set as ancillary.

  • q_max – The maximum index of the circuit qubits to set as ancillary.

is_circuit_qubit_ancillary(q: int) bool

Check if a circuit (i.e., logical) qubit is ancillary.

Parameters:

q – The index of the circuit qubit to check.

Returns:

True if the circuit qubit is ancillary, False otherwise.

set_circuit_qubit_garbage(q: int) None

Set a circuit (i.e., logical) qubit to be garbage.

Parameters:

q – The index of the circuit qubit to set as garbage.

set_circuit_qubits_garbage(q_min: int, q_max: int) None

Set a range of circuit (i.e., logical) qubits to be garbage.

Parameters:
  • q_min – The minimum index of the circuit qubits to set as garbage.

  • q_max – The maximum index of the circuit qubits to set as garbage.

is_circuit_qubit_garbage(q: int) bool

Check if a circuit (i.e., logical) qubit is garbage.

Parameters:

q – The index of the circuit qubit to check.

Returns:

True if the circuit qubit is garbage, False otherwise.

add_variable(var: Expression | float) None

Add a variable to the quantum computation.

Parameters:

var – The variable to add.

add_variables(vars_: Sequence[Expression | float]) None

Add multiple variables to the quantum computation.

Parameters:

vars – The variables to add.

is_variable_free() bool

Check if the quantum computation is free of variables.

Returns:

True if the quantum computation is free of variables, False otherwise.

instantiate(assignment: Mapping[Variable, float]) QuantumComputation

Instantiate the quantum computation with the given variable assignment.

Parameters:

assignment – The variable assignment to instantiate the quantum computation with.

Returns:

The instantiated quantum computation.

instantiate_inplace(assignment: Mapping[Variable, float]) None

Instantiate the quantum computation with the given variable assignment in-place.

Parameters:

assignment – The variable assignment to instantiate the quantum computation with.

qasm2_str() str

Return the OpenQASM2 representation of the quantum computation as a string.

Note

This uses some custom extensions to OpenQASM 2.0 that allow for easier definition of multi-controlled gates. These extensions might not be supported by all OpenQASM 2.0 parsers. Consider using the qasm3_str() method instead, which uses OpenQASM 3.0 that natively supports multi-controlled gates. The export also assumes the bigger, non-standard qelib1.inc from Qiskit is available.

Returns:

The OpenQASM2 representation of the quantum computation as a string.

qasm2(filename: PathLike[str] | str) None

Write the OpenQASM2 representation of the quantum computation to a file.

See also

qasm2_str()

Parameters:

filename – The filename of the file to write the OpenQASM2 representation to.

qasm3_str() str

Return the OpenQASM3 representation of the quantum computation as a string.

Returns:

The OpenQASM3 representation of the quantum computation as a string.

qasm3(filename: PathLike[str] | str) None

Write the OpenQASM3 representation of the quantum computation to a file.

See also

qasm3_str()

Parameters:

filename – The filename of the file to write the OpenQASM3 representation to.

i(q: int) None

Apply an identity operation.

\[\begin{split}I = \begin{pmatrix} 1 & 0 \\ 0 & 1 \end{pmatrix}\end{split}\]
Parameters:

q – The target qubit

ci(control: Control | int, target: int) None

Apply a controlled identity operation.

Parameters:
  • control – The control qubit

  • target – The target qubit

See also

i()

mci(controls: set[Control | int], target: int) None

Apply a multi-controlled identity operation.

Parameters:
  • controls – The control qubits

  • target – The target qubit

See also

i()

x(q: int) None

Apply a Pauli-X gate.

\[\begin{split}X = \begin{pmatrix} 0 & 1 \\ 1 & 0 \end{pmatrix}\end{split}\]
Parameters:

q – The target qubit

cx(control: Control | int, target: int) None

Apply a controlled Pauli-X (CNOT / CX) gate.

Parameters:
  • control – The control qubit

  • target – The target qubit

See also

x()

mcx(controls: set[Control | int], target: int) None

Apply a multi-controlled Pauli-X (Toffoli / MCX) gate.

Parameters:
  • controls – The control qubits

  • target – The target qubit

See also

x()

y(q: int) None

Apply a Pauli-Y gate.

\[\begin{split}Y = \begin{pmatrix} 0 & -i \\ i & 0 \end{pmatrix}\end{split}\]
Parameters:

q – The target qubit

cy(control: Control | int, target: int) None

Apply a controlled Pauli-Y gate.

Parameters:
  • control – The control qubit

  • target – The target qubit

See also

y()

mcy(controls: set[Control | int], target: int) None

Apply a multi-controlled Pauli-Y gate.

Parameters:
  • controls – The control qubits

  • target – The target qubit

See also

y()

z(q: int) None

Apply a Pauli-Z gate.

\[\begin{split}Z = \begin{pmatrix} 1 & 0 \\ 0 & -1 \end{pmatrix}\end{split}\]
Parameters:

q – The target qubit

cz(control: Control | int, target: int) None

Apply a controlled Pauli-Z (CZ) gate.

Parameters:
  • control – The control qubit

  • target – The target qubit

See also

z()

mcz(controls: set[Control | int], target: int) None

Apply a multi-controlled Pauli-Z (MCZ) gate.

Parameters:
  • controls – The control qubits

  • target – The target qubit

See also

z()

h(q: int) None

Apply a Hadamard gate.

\[\begin{split}H = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & 1 \\ 1 & -1 \end{pmatrix}\end{split}\]
Parameters:

q – The target qubit

ch(control: Control | int, target: int) None

Apply a controlled Hadamard gate.

Parameters:
  • control – The control qubit

  • target – The target qubit

See also

h()

mch(controls: set[Control | int], target: int) None

Apply a multi-controlled Hadamard gate.

Parameters:
  • controls – The control qubits

  • target – The target qubit

See also

h()

s(q: int) None

Apply an S gate (phase gate).

\[\begin{split}S = \begin{pmatrix} 1 & 0 \\ 0 & i \end{pmatrix}\end{split}\]
Parameters:

q – The target qubit

cs(control: Control | int, target: int) None

Apply a controlled S gate (CS gate).

Parameters:
  • control – The control qubit

  • target – The target qubit

See also

s()

mcs(controls: set[Control | int], target: int) None

Apply a multi-controlled S gate.

Parameters:
  • controls – The control qubits

  • target – The target qubit

See also

s()

sdg(q: int) None

Apply an \(S^{\dagger}\) gate.

\[\begin{split}S^{\dagger} = \begin{pmatrix} 1 & 0 \\ 0 & -i \end{pmatrix}\end{split}\]
Parameters:

q – The target qubit

csdg(control: Control | int, target: int) None

Apply a controlled \(S^{\dagger}\) gate.

Parameters:
  • control – The control qubit

  • target – The target qubit

See also

sdg()

mcsdg(controls: set[Control | int], target: int) None

Apply a multi-controlled \(S^{\dagger}\) gate.

Parameters:
  • controls – The control qubits

  • target – The target qubit

See also

sdg()

t(q: int) None

Apply a T gate.

\[\begin{split}T = \begin{pmatrix} 1 & 0 \\ 0 & e^{i\pi/4} \end{pmatrix}\end{split}\]
Parameters:

q – The target qubit

ct(control: Control | int, target: int) None

Apply a controlled T gate.

Parameters:
  • control – The control qubit

  • target – The target qubit

See also

t()

mct(controls: set[Control | int], target: int) None

Apply a multi-controlled T gate.

Parameters:
  • controls – The control qubits

  • target – The target qubit

See also

t()

tdg(q: int) None

Apply a \(T^{\dagger}\) gate.

\[\begin{split}T^{\dagger} = \begin{pmatrix} 1 & 0 \\ 0 & e^{-i\pi/4} \end{pmatrix}\end{split}\]
Parameters:

q – The target qubit

ctdg(control: Control | int, target: int) None

Apply a controlled \(T^{\dagger}\) gate.

Parameters:
  • control – The control qubit

  • target – The target qubit

See also

tdg()

mctdg(controls: set[Control | int], target: int) None

Apply a multi-controlled \(T^{\dagger}\) gate.

Parameters:
  • controls – The control qubits

  • target – The target qubit

See also

tdg()

v(q: int) None

Apply a V gate.

\[\begin{split}V = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & -i \\ -i & 1 \end{pmatrix}\end{split}\]
Parameters:

q – The target qubit

cv(control: Control | int, target: int) None

Apply a controlled V gate.

Parameters:
  • control – The control qubit

  • target – The target qubit

See also

v()

mcv(controls: set[Control | int], target: int) None

Apply a multi-controlled V gate.

Parameters:
  • controls – The control qubits

  • target – The target qubit

See also

v()

vdg(q: int) None

Apply a \(V^{\dagger}\) gate.

\[\begin{split}V^{\dagger} = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & i \\ i & 1 \end{pmatrix}\end{split}\]
Parameters:

q – The target qubit

cvdg(control: Control | int, target: int) None

Apply a controlled \(V^{\dagger}\) gate.

Parameters:
  • control – The control qubit

  • target – The target qubit

See also

vdg()

mcvdg(controls: set[Control | int], target: int) None

Apply a multi-controlled \(V^{\dagger}\) gate.

Parameters:
  • controls – The control qubits

  • target – The target qubit

See also

vdg()

sx(q: int) None

Apply a \(\sqrt{X}\) gate.

\[\begin{split}\sqrt{X} = \frac{1}{2} \begin{pmatrix} 1 + i & 1 - i \\ 1 - i & 1 + i \end{pmatrix}\end{split}\]
Parameters:

q – The target qubit

csx(control: Control | int, target: int) None

Apply a controlled \(\sqrt{X}\) gate.

Parameters:
  • control – The control qubit

  • target – The target qubit

See also

sx()

mcsx(controls: set[Control | int], target: int) None

Apply a multi-controlled \(\sqrt{X}\) gate.

Parameters:
  • controls – The control qubits

  • target – The target qubit

See also

sx()

sxdg(q: int) None

Apply a \(\sqrt{X}^{\dagger}\) gate.

\[\begin{split}\sqrt{X}^{\dagger} = \frac{1}{2} \begin{pmatrix} 1 - i & 1 + i \\ 1 + i & 1 - i \end{pmatrix}\end{split}\]
Parameters:

q – The target qubit

csxdg(control: Control | int, target: int) None

Apply a controlled \(\sqrt{X}^{\dagger}\) gate.

Parameters:
  • control – The control qubit

  • target – The target qubit

See also

sxdg()

mcsxdg(controls: set[Control | int], target: int) None

Apply a multi-controlled \(\sqrt{X}^{\dagger}\) gate.

Parameters:
  • controls – The control qubits

  • target – The target qubit

See also

sxdg()

rx(theta: float | Expression, q: int) None

Apply an \(R_x(\theta)\) gate.

\[\begin{split}R_x(\theta) = e^{-i\theta X/2} = \cos(\theta/2) I - i \sin(\theta/2) X = \begin{pmatrix} \cos(\theta/2) & -i \sin(\theta/2) \\ -i \sin(\theta/2) & \cos(\theta/2) \end{pmatrix}\end{split}\]
Parameters:
  • theta – The rotation angle

  • q – The target qubit

crx(theta: float | Expression, control: Control | int, target: int) None

Apply a controlled \(R_x(\theta)\) gate.

Parameters:
  • theta – The rotation angle

  • control – The control qubit

  • target – The target qubit

See also

rx()

mcrx(theta: float | Expression, controls: set[Control | int], target: int) None

Apply a multi-controlled \(R_x(\theta)\) gate.

Parameters:
  • theta – The rotation angle

  • controls – The control qubits

  • target – The target qubit

See also

rx()

ry(theta: float | Expression, q: int) None

Apply an \(R_y(\theta)\) gate.

\[\begin{split}R_y(\theta) = e^{-i\theta Y/2} = \cos(\theta/2) I - i \sin(\theta/2) Y = \begin{pmatrix} \cos(\theta/2) & -\sin(\theta/2) \\ \sin(\theta/2) & \cos(\theta/2) \end{pmatrix}\end{split}\]
Parameters:
  • theta – The rotation angle

  • q – The target qubit

cry(theta: float | Expression, control: Control | int, target: int) None

Apply a controlled \(R_y(\theta)\) gate.

Parameters:
  • theta – The rotation angle

  • control – The control qubit

  • target – The target qubit

See also

ry()

mcry(theta: float | Expression, controls: set[Control | int], target: int) None

Apply a multi-controlled \(R_y(\theta)\) gate.

Parameters:
  • theta – The rotation angle

  • controls – The control qubits

  • target – The target qubit

See also

ry()

rz(theta: float | Expression, q: int) None

Apply an \(R_z(\theta)\) gate.

\[\begin{split}R_z(\theta) = e^{-i\theta Z/2} = \begin{pmatrix} e^{-i\theta/2} & 0 \\ 0 & e^{i\theta/2} \end{pmatrix}\end{split}\]
Parameters:
  • theta – The rotation angle

  • q – The target qubit

crz(theta: float | Expression, control: Control | int, target: int) None

Apply a controlled \(R_z(\theta)\) gate.

Parameters:
  • theta – The rotation angle

  • control – The control qubit

  • target – The target qubit

See also

rz()

mcrz(theta: float | Expression, controls: set[Control | int], target: int) None

Apply a multi-controlled \(R_z(\theta)\) gate.

Parameters:
  • theta – The rotation angle

  • controls – The control qubits

  • target – The target qubit

See also

rz()

p(theta: float | Expression, q: int) None

Apply a phase gate.

\[\begin{split}P(\theta) = \begin{pmatrix} 1 & 0 \\ 0 & e^{i\theta} \end{pmatrix}\end{split}\]
Parameters:
  • theta – The rotation angle

  • q – The target qubit

cp(theta: float | Expression, control: Control | int, target: int) None

Apply a controlled phase gate.

Parameters:
  • theta – The rotation angle

  • control – The control qubit

  • target – The target qubit

See also

p()

mcp(theta: float | Expression, controls: set[Control | int], target: int) None

Apply a multi-controlled phase gate.

Parameters:
  • theta – The rotation angle

  • controls – The control qubits

  • target – The target qubit

See also

p()

u2(phi: float | Expression, lambda_: float | Expression, q: int) None

Apply a \(U_2(\phi, \lambda)\) gate.

\[\begin{split}U_2(\phi, \lambda) = \frac{1}{\sqrt{2}} \begin{pmatrix} 1 & -e^{i\lambda} \\ e^{i\phi} & e^{i(\phi + \lambda)} \end{pmatrix}\end{split}\]
Parameters:
  • phi – The rotation angle

  • lambda – The rotation angle

  • q – The target qubit

cu2(phi: float | Expression, lambda_: float | Expression, control: Control | int, target: int) None

Apply a controlled \(U_2(\phi, \lambda)\) gate.

Parameters:
  • phi – The rotation angle

  • lambda – The rotation angle

  • control – The control qubit

  • target – The target qubit

See also

u2()

mcu2(phi: float | Expression, lambda_: float | Expression, controls: set[Control | int], target: int) None

Apply a multi-controlled \(U_2(\phi, \lambda)\) gate.

Parameters:
  • phi – The rotation angle

  • lambda – The rotation angle

  • controls – The control qubits

  • target – The target qubit

See also

u2()

u(theta: float | Expression, phi: float | Expression, lambda_: float | Expression, q: int) None

Apply a \(U(\theta, \phi, \lambda)\) gate.

\[\begin{split}U(\theta, \phi, \lambda) = \begin{pmatrix} \cos(\theta/2) & -e^{i\lambda}\sin(\theta/2) \\ e^{i\phi}\sin(\theta/2) & e^{i(\phi + \lambda)}\cos(\theta/2) \end{pmatrix}\end{split}\]
Parameters:
  • theta – The rotation angle

  • phi – The rotation angle

  • lambda – The rotation angle

  • q – The target qubit

cu(theta: float | Expression, phi: float | Expression, lambda_: float | Expression, control: Control | int, target: int) None

Apply a controlled \(U(\theta, \phi, \lambda)\) gate.

Parameters:
  • theta – The rotation angle

  • phi – The rotation angle

  • lambda – The rotation angle

  • control – The control qubit

  • target – The target qubit

See also

u()

mcu(theta: float | Expression, phi: float | Expression, lambda_: float | Expression, controls: set[Control | int], target: int) None

Apply a multi-controlled \(U(\theta, \phi, \lambda)\) gate.

Parameters:
  • theta – The rotation angle

  • phi – The rotation angle

  • lambda – The rotation angle

  • controls – The control qubits

  • target – The target qubit

See also

u()

swap(target1: int, target2: int) None

Apply a SWAP gate.

\[\begin{split}SWAP = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}\end{split}\]
Parameters:
  • target1 – The first target qubit

  • target2 – The second target qubit

cswap(control: Control | int, target1: int, target2: int) None

Apply a controlled SWAP gate.

Parameters:
  • control – The control qubit

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

swap()

mcswap(controls: set[Control | int], target1: int, target2: int) None

Apply a multi-controlled SWAP gate.

Parameters:
  • controls – The control qubits

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

swap()

dcx(target1: int, target2: int) None

Apply a DCX (double CNOT) gate.

\[\begin{split}DCX = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \end{pmatrix}\end{split}\]
Parameters:
  • target1 – The first target qubit

  • target2 – The second target qubit

cdcx(control: Control | int, target1: int, target2: int) None

Apply a controlled DCX (double CNOT) gate.

Parameters:
  • control – The control qubit

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

dcx()

mcdcx(controls: set[Control | int], target1: int, target2: int) None

Apply a multi-controlled DCX (double CNOT) gate.

Parameters:
  • controls – The control qubits

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

dcx()

ecr(target1: int, target2: int) None

Apply an ECR (echoed cross-resonance) gate.

\[\begin{split}ECR = \frac{1}{\sqrt{2}} \begin{pmatrix} 0 & 0 & 1 & i \\ 0 & 0 & i & 1 \\ 1 & -i & 0 & 0 \\ -i & 1 & 0 & 0 \end{pmatrix}\end{split}\]
Parameters:
  • target1 – The first target qubit

  • target2 – The second target qubit

cecr(control: Control | int, target1: int, target2: int) None

Apply a controlled ECR (echoed cross-resonance) gate.

Parameters:
  • control – The control qubit

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

ecr()

mcecr(controls: set[Control | int], target1: int, target2: int) None

Apply a multi-controlled ECR (echoed cross-resonance) gate.

Parameters:
  • controls – The control qubits

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

ecr()

iswap(target1: int, target2: int) None

Apply an iSWAP gate.

\[\begin{split}iSWAP = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & i & 0 \\ 0 & i & 0 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}\end{split}\]
Parameters:
  • target1 – The first target qubit

  • target2 – The second target qubit

ciswap(control: Control | int, target1: int, target2: int) None

Apply a controlled iSWAP gate.

Parameters:
  • control – The control qubit

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

iswap()

mciswap(controls: set[Control | int], target1: int, target2: int) None

Apply a multi-controlled iSWAP gate.

Parameters:
  • controls – The control qubits

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

iswap()

iswapdg(target1: int, target2: int) None

Apply an \(iSWAP^{\dagger}\) gate.

\[\begin{split}iSWAP^{\dagger} = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & -i & 0 \\ 0 & -i & 0 & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}\end{split}\]
Parameters:
  • target1 – The first target qubit

  • target2 – The second target qubit

ciswapdg(control: Control | int, target1: int, target2: int) None

Apply a controlled \(iSWAP^{\dagger}\) gate.

Parameters:
  • control – The control qubit

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

iswapdg()

mciswapdg(controls: set[Control | int], target1: int, target2: int) None

Apply a multi-controlled \(iSWAP^{\dagger}\) gate.

Parameters:
  • controls – The control qubits

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

iswapdg()

peres(target1: int, target2: int) None

Apply a Peres gate.

\[\begin{split}Peres = \begin{pmatrix} 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \\ 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \end{pmatrix}\end{split}\]
Parameters:
  • target1 – The first target qubit

  • target2 – The second target qubit

cperes(control: Control | int, target1: int, target2: int) None

Apply a controlled Peres gate.

Parameters:
  • control – The control qubit

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

peres()

mcperes(controls: set[Control | int], target1: int, target2: int) None

Apply a multi-controlled Peres gate.

Parameters:
  • controls – The control qubits

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

peres()

peresdg(target1: int, target2: int) None

Apply a \(Peres^{\dagger}\) gate.

\[\begin{split}Peres^{\dagger} = \begin{pmatrix} 0 & 0 & 1 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 1 & 0 & 0 \\ 1 & 0 & 0 & 0 \end{pmatrix}\end{split}\]
Parameters:
  • target1 – The first target qubit

  • target2 – The second target qubit

cperesdg(control: Control | int, target1: int, target2: int) None

Apply a controlled \(Peres^{\dagger}\) gate.

Parameters:
  • control – The control qubit

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

peresdg()

mcperesdg(controls: set[Control | int], target1: int, target2: int) None

Apply a multi-controlled \(Peres^{\dagger}\) gate.

Parameters:
  • controls – The control qubits

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

peresdg()

rxx(theta: float | Expression, target1: int, target2: int) None

Apply an \(R_{xx}(\theta)\) gate.

\[\begin{split}R_{xx}(\theta) = e^{-i\theta XX/2} = \cos(\theta/2) I\otimes I - i \sin(\theta/2) X \otimes X = \begin{pmatrix} \cos(\theta/2) & 0 & 0 & -i \sin(\theta/2) \\ 0 & \cos(\theta/2) & -i \sin(\theta/2) & 0 \\ 0 & -i \sin(\theta/2) & \cos(\theta/2) & 0 \\ -i \sin(\theta/2) & 0 & 0 & \cos(\theta/2) \end{pmatrix}\end{split}\]
Parameters:
  • theta – The rotation angle

  • target1 – The first target qubit

  • target2 – The second target qubit

crxx(theta: float | Expression, control: Control | int, target1: int, target2: int) None

Apply a controlled \(R_{xx}(\theta)\) gate.

Parameters:
  • theta – The rotation angle

  • control – The control qubit

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

rxx()

mcrxx(theta: float | Expression, controls: set[Control | int], target1: int, target2: int) None

Apply a multi-controlled \(R_{xx}(\theta)\) gate.

Parameters:
  • theta – The rotation angle

  • controls – The control qubits

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

rxx()

ryy(theta: float | Expression, target1: int, target2: int) None

Apply an \(R_{yy}(\theta)\) gate.

\[\begin{split}R_{yy}(\theta) = e^{-i\theta YY/2} = \cos(\theta/2) I\otimes I - i \sin(\theta/2) Y \otimes Y = \begin{pmatrix} \cos(\theta/2) & 0 & 0 & i \sin(\theta/2) \\ 0 & \cos(\theta/2) & -i \sin(\theta/2) & 0 \\ 0 & -i \sin(\theta/2) & \cos(\theta/2) & 0 \\ i \sin(\theta/2) & 0 & 0 & \cos(\theta/2) \end{pmatrix}\end{split}\]
Parameters:
  • theta – The rotation angle

  • target1 – The first target qubit

  • target2 – The second target qubit

cryy(theta: float | Expression, control: Control | int, target1: int, target2: int) None

Apply a controlled \(R_{yy}(\theta)\) gate.

Parameters:
  • theta – The rotation angle

  • control – The control qubit

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

ryy()

mcryy(theta: float | Expression, controls: set[Control | int], target1: int, target2: int) None

Apply a multi-controlled \(R_{yy}(\theta)\) gate.

Parameters:
  • theta – The rotation angle

  • controls – The control qubits

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

ryy()

rzz(theta: float | Expression, target1: int, target2: int) None

Apply an \(R_{zz}(\theta)\) gate.

\[\begin{split}R_{zz}(\theta) = e^{-i\theta ZZ/2} = \begin{pmatrix} e^{-i\theta/2} & 0 & 0 & 0 \\ 0 & e^{i\theta/2} & 0 & 0 \\ 0 & 0 & e^{i\theta/2} & 0 \\ 0 & 0 & 0 & e^{-i\theta/2} \end{pmatrix}\end{split}\]
Parameters:
  • theta – The rotation angle

  • target1 – The first target qubit

  • target2 – The second target qubit

crzz(theta: float | Expression, control: Control | int, target1: int, target2: int) None

Apply a controlled \(R_{zz}(\theta)\) gate.

Parameters:
  • theta – The rotation angle

  • control – The control qubit

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

rzz()

mcrzz(theta: float | Expression, controls: set[Control | int], target1: int, target2: int) None

Apply a multi-controlled \(R_{zz}(\theta)\) gate.

Parameters:
  • theta – The rotation angle

  • controls – The control qubits

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

rzz()

rzx(theta: float | Expression, target1: int, target2: int) None

Apply an \(R_{zx}(\theta)\) gate.

\[\begin{split}R_{zx}(\theta) = e^{-i\theta ZX/2} = \cos(\theta/2) I\otimes I - i \sin(\theta/2) Z \otimes X = \begin{pmatrix} \cos(\theta/2) & -i \sin(\theta/2) & 0 & 0 \\ -i \sin(\theta/2) & \cos(\theta/2) & 0 & 0 \\ 0 & 0 & \cos(\theta/2) & i \sin(\theta/2) \\ 0 & 0 & i \sin(\theta/2) & \cos(\theta/2) \end{pmatrix}\end{split}\]
Parameters:
  • theta – The rotation angle

  • target1 – The first target qubit

  • target2 – The second target qubit

crzx(theta: float | Expression, control: Control | int, target1: int, target2: int) None

Apply a controlled \(R_{zx}(\theta)\) gate.

Parameters:
  • theta – The rotation angle

  • control – The control qubit

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

rzx()

mcrzx(theta: float | Expression, controls: set[Control | int], target1: int, target2: int) None

Apply a multi-controlled \(R_{zx}(\theta)\) gate.

Parameters:
  • theta – The rotation angle

  • controls – The control qubits

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

rzx()

xx_minus_yy(theta: float | Expression, beta: float | Expression, target1: int, target2: int) None

Apply an \(R_{XX - YY}(\theta, \beta)\) gate.

\[\begin{split}R_{XX - YY}(\theta, \beta) = R_{z_2}(\beta) \cdot e^{-i\frac{\theta}{2} \frac{XX-YY}{2}} \cdot R_{z_2}(-\beta) = \begin{pmatrix} \cos(\theta/2) & 0 & 0 & -i \sin(\theta/2) e^{-i\beta} \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 1 & 0 \\ -i \sin(\theta/2) e^{i\beta} & 0 & 0 & \cos(\theta/2) \end{pmatrix}\end{split}\]
Parameters:
  • theta – The rotation angle

  • beta – The rotation angle

  • target1 – The first target qubit

  • target2 – The second target qubit

cxx_minus_yy(theta: float | Expression, beta: float | Expression, control: Control | int, target1: int, target2: int) None

Apply a controlled \(R_{XX - YY}(\theta, \beta)\) gate.

Parameters:
  • theta – The rotation angle

  • beta – The rotation angle

  • control – The control qubit

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

xx_minus_yy()

mcxx_minus_yy(theta: float | Expression, beta: float | Expression, controls: set[Control | int], target1: int, target2: int) None

Apply a multi-controlled \(R_{XX - YY}(\theta, \beta)\) gate.

Parameters:
  • theta – The rotation angle

  • beta – The rotation angle

  • controls – The control qubits

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

xx_minus_yy()

xx_plus_yy(theta: float | Expression, beta: float | Expression, target1: int, target2: int) None

Apply an \(R_{XX + YY}(\theta, \beta)\) gate.

\[\begin{split}R_{XX + YY}(\theta, \beta) = R_{z_1}(\beta) \cdot e^{-i\frac{\theta}{2} \frac{XX+YY}{2}} \cdot R_{z_1}(-\beta) = \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & \cos(\theta/2) & -i \sin(\theta/2) e^{-i\beta} & 0 \\ 0 & -i \sin(\theta/2) e^{i\beta} & \cos(\theta/2) & 0 \\ 0 & 0 & 0 & 1 \end{pmatrix}\end{split}\]
Parameters:
  • theta – The rotation angle

  • beta – The rotation angle

  • target1 – The first target qubit

  • target2 – The second target qubit

cxx_plus_yy(theta: float | Expression, beta: float | Expression, control: Control | int, target1: int, target2: int) None

Apply a controlled \(R_{XX + YY}(\theta, \beta)\) gate.

Parameters:
  • theta – The rotation angle

  • beta – The rotation angle

  • control – The control qubit

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

xx_plus_yy()

mcxx_plus_yy(theta: float | Expression, beta: float | Expression, controls: set[Control | int], target1: int, target2: int) None

Apply a multi-controlled \(R_{XX + YY}(\theta, \beta)\) gate.

Parameters:
  • theta – The rotation angle

  • beta – The rotation angle

  • controls – The control qubits

  • target1 – The first target qubit

  • target2 – The second target qubit

See also

xx_plus_yy()

gphase(theta: float) None

Apply a global phase gate.

\[GPhase(\theta) = (e^{i\theta})\]
Parameters:

theta – The rotation angle

measure(qubit: int, cbit: int) None
measure(qubit: int, creg_bit: tuple[str, int]) None
measure(qubits: Sequence[int], cbits: Sequence[int]) None

Measure multiple qubits and store the results in classical bits.

This method is equivalent to calling measure() multiple times.

Parameters:
  • qubits – The qubits to measure

  • cbits – The classical bits to store the results

measure_all(add_bits: bool = True) None

Measure all qubits and store the results in classical bits.

Details:

If add_bits is True, a new classical register (named “meas”) with the same size as the number of qubits will be added to the circuit and the results will be stored in it. If add_bits is False, the classical register must already exist and have a sufficient number of bits to store the results.

Parameters:

add_bits – Whether to explicitly add a classical register

reset(q: int) None
reset(qubits: Sequence[int]) None

Add a reset operation to the circuit.

Parameters:

qubits – The qubits to reset

barrier() None
barrier(q: int) None
barrier(qubits: Sequence[int]) None

Add a barrier to the circuit.

Parameters:

qubits – The qubits to add the barrier to

classic_controlled(op: OpType, target: int, creg: tuple[int, int], expected_value: int = 1, params: Sequence[float] = ()) None
classic_controlled(op: OpType, target: int, control: Control | int, creg: tuple[int, int], expected_value: int = 1, params: Sequence[float] = ()) None
classic_controlled(op: OpType, target: int, controls: set[Control | int], creg: tuple[int, int], expected_value: int = 1, params: Sequence[float] = ()) None

Add a classic-controlled operation to the circuit.

Parameters:
  • op – The operation to apply

  • target – The target qubit

  • controls – The control qubits

  • creg – The classical register (index and number of bits)

  • expected_value – The expected value of the classical register

  • params – The parameters of the operation

__version__: str
version_info: tuple[int, int, int, str, str] | tuple[int, int, int]