frontier.utils.quantumcircuit

Classes

QasmEmitter(options)

Translate a QuantumCircuit into OpenQASM source code.

QasmEmitterOptions([format, target_sdk, ...])

Configuration options that control how a QasmEmitter translates a QuantumCircuit into OpenQASM source.

QuantumCircuit([number_of_qubits, ...])

In-memory representation of a quantum circuit.

QuantumGate(name, target_qubits[, parameters])

Data container for a (possibly parameterized) quantum gate.

TwoQubitQuantumGate(name, target_qubits, ...)

A controlled or otherwise two‑qubit gate.

class QasmEmitter(options: QasmEmitterOptions)[source]

Bases: object

Translate a QuantumCircuit into OpenQASM source code.

Responsibilities:
  • Header emission (version + includes)

  • Register declarations

  • Gate translation & parameter formatting

  • Measurements and resets

emit(circuit: QuantumCircuit) str[source]

Generate a full QASM program for circuit.

Returns:

The QASM source code as a single string.

class QasmEmitterOptions(format: str = 'qasm2', target_sdk: str | None = None, includes: str | Sequence[str] | None = None, float_precision: int = 6, custom_template: Dict[str, Any] | None = None)[source]

Bases: object

Configuration options that control how a QasmEmitter translates a QuantumCircuit into OpenQASM source.

format

OpenQASM version to target. Supported values are "qasm2" and "qasm3".

Type:

str

target_sdk

Optional SDK dialect for gate aliases. If provided, it adjusts emitted gate names to match that SDK. Supported values: "qiskit", "braket", "tket", or "custom". None means use the format’s default aliasing.

Type:

str | None

includes

Optional include file(s). May be a single string path or a sequence of paths. If target_sdk is one of the known SDKs, this is automatically populated with sensible defaults for the selected format.

Type:

str | collections.abc.Sequence[str] | None

float_precision

Number of decimal places when formatting floating parameters in QASM output.

Type:

int

custom_template

Optional mapping of internal gate names to the desired QASM names that overrides the selected format/SDK template.

Type:

Dict[str, Any] | None

map

(Computed) Effective internal-gate → QASM-gate alias mapping.

Type:

Dict[str, str]

get_qasm_name(internal_name: str) str[source]

Translate an internal gate name to its QASM alias.

If the gate is not present in the mapping, the original name is returned unchanged.

class QuantumCircuit(number_of_qubits: int = 0, number_of_classical_bits: int = 0)[source]

Bases: object

In-memory representation of a quantum circuit.

The circuit stores qubit/bit registers plus an ordered list of gate operations, measurements, and resets. Convenience helpers are provided to append common gates and to export the circuit to OpenQASM.

number_of_qubits

Total number of qubits in the circuit.

number_of_classical_bits

Total number of classical bits in the circuit.

gate_list

Ordered list of gate objects.

measurements

(qubit, cbit) pairs describing where measurement outcomes are stored.

reset_operations

Indices of qubits that are reset.

add_classical_bits(additional_classical_bits: int) None[source]

Increase the number of classical bits by additional_classical_bits.

add_cx_gate(control_qubit: int, target_qubit: int) None[source]

Append a controlled-X (CNOT) with control_qubit target_qubit.

add_cy_gate(control_qubit: int, target_qubit: int) None[source]

Append a controlled-Y with control_qubit target_qubit.

add_cz_gate(control_qubit: int, target_qubit: int) None[source]

Append a controlled-Z with control_qubit target_qubit.

add_h_gate(qubit: int) None[source]

Append a Hadamard gate on qubit.

add_measurement(qubit: int | Sequence[int], classical_bit: int | Sequence[int]) None[source]

Append measurement(s).

Supports scalar ints, lists/tuples, numpy arrays, and range().

Rules:
  • If both are sequences, they must have equal length (pairwise mapping).

  • If qubit is a sequence and classical_bit is scalar, classical_bit is reused.

  • If qubit is scalar and classical_bit is sequence, classical_bit must be length 1 (or you can decide to error).

add_qubits(additional_qubits: int) None[source]

Increase the number of qubits by additional_qubits.

add_reset_operation(qubit: int) None[source]

Append a reset operation for the specified qubit.

add_rx_gate(qubit: int, theta: float) None[source]

Append an RX rotation of angle theta on qubit.

add_ry_gate(qubit: int, theta: float) None[source]

Append an RY rotation of angle theta on qubit.

add_rz_gate(qubit: int, theta: float) None[source]

Append an RZ rotation of angle theta on qubit.

add_s_gate(qubit: int) None[source]

Append an S gate on qubit.

add_sdg_gate(qubit: int) None[source]

Append an S gate on qubit.

add_swap_gate(qubit_one: int, qubit_two: int) None[source]

Append a SWAP gate between qubit_one and qubit_two.

Notes

Internally this is modeled as a two-qubit gate and emitted as swap q[qubit_one], q[qubit_two]; in QASM.

add_t_gate(qubit: int) None[source]

Append a T gate on qubit.

add_u_gate(qubit: int, theta: float, phi: float, lambda_parameter: float) None[source]

Append a general single-qubit unitary gate u(theta, phi, lambda).

Parameters:
  • qubit – Target qubit.

  • theta – First Euler angle.

  • phi – Second Euler angle.

  • lambda_parameter – Third Euler angle.

add_x_gate(qubit: int) None[source]

Append an X gate on qubit.

add_y_gate(qubit: int) None[source]

Append a Y gate on qubit.

add_z_gate(qubit: int) None[source]

Append a Z gate on qubit.

display_gate_descriptions() None[source]

Print a short description for each supported internal gate name.

draw_circuit_diagram(max_length: int = 20) None[source]

Print a simple ASCII diagram of the circuit.

The diagram is split into vertical blocks if any line exceeds max_length gate-units.

Parameters:

max_length – Maximum number of gate-units per block.

single_qubit_gate_count() int[source]

Return the number of single-qubit gate applications in the circuit.

to_qasm(emitter_options: QasmEmitterOptions | None = None, *, format: str = 'qasm2', target_sdk: str | None = None, **kwargs: Any) str[source]

Generate OpenQASM code for the circuit.

Parameters:
  • emitter_options – Pre-configured options. If omitted, a new QasmEmitterOptions is created from format, target_sdk and any extra kwargs.

  • format – QASM format used when building the default options.

  • target_sdk – Optional SDK aliasing used when building default options.

  • **kwargs – Passed to QasmEmitterOptions.

Returns:

The QASM program as a multi-line str.

to_unitary() ndarray[source]

Compute the overall unitary matrix for the circuit.

Raises:

ValueError

  • if number_of_qubits > 10 - if number of gates > 100

two_qubit_gate_count() int[source]

Return the number of two-qubit gate applications in the circuit.

class QuantumGate(name: str, target_qubits: int | Sequence[int], parameters: float | Sequence[float] | None = None)[source]

Bases: object

Data container for a (possibly parameterized) quantum gate.

name

Internal gate name (e.g., "x", "rz", "cx").

Type:

str

target_qubits

Index/indices of the qubits this gate acts on.

Type:

int | collections.abc.Sequence[int]

parameters

Optional numeric parameter or sequence of parameters. Values are normalized to a list of float.

Type:

float | collections.abc.Sequence[float] | None

property is_two_qubit_gate: bool

False.

Type:

Return True if this gate is multi-qubit/controlled. Base

class TwoQubitQuantumGate(name: str, target_qubits: int | ~collections.abc.Sequence[int], parameters: float | ~collections.abc.Sequence[float] | None = None, control_qubits: int | ~collections.abc.Sequence[int] = <factory>)[source]

Bases: QuantumGate

A controlled or otherwise two‑qubit gate.

control_qubits

Index/indices of control qubits. At least one is required.

Type:

int | collections.abc.Sequence[int]

property is_two_qubit_gate: bool

Return True for this subclass.