qpandalite.circuit_builder package

Quantum Circuit Module

qpandalite.circuit_builder.qcircuit module

class qpandalite.circuit_builder.qcircuit.Circuit[source]

Definition of quantum circuit (Circuit).

Class Circuit acts as the OriginIR generator and analysis tool supported by origin_line_parser and origin_base_parser. Each function within the class provides the necessary components to construct quantum circuits. After parsing, each line is transferred to either the OriginIR_Simulator, OriginIR_Parser, OpenQASM2_Parser, or actual quantum machines for execution or further processing.

used_qubit_list

A list to keep track of the qubits used in the circuit.

Type:

list

circuit_str

The string representation of the circuit in OriginIR format.

Type:

str

max_qubit

The maximum index of qubits used in the circuit.

Type:

int

measure_list

A list of qubits that will be measured.

Type:

list

circuit_info

A dictionary containing information about the circuit such as the number of qubits, the types and counts of gates used, and the measurement setup.

Type:

dict

analyze_circuit()[source]

Analyzes the stored string representation of a quantum circuit and updates the circuit information.

The updated ‘circuit_info’ dictionary contains: - ‘qubits’: The number of qubits used in the circuit. - ‘gates’: A dictionary with types of gates used and their counts. - ‘measurements’: Information about the measurement setup of the circuit.

Parameters:

circuit_str (str) – A string representation of the quantum circuit to be analyzed.

Returns:

A dictionary containing information about the quantum circuit, including: - ‘qubits’: an integer representing the number of qubits. - ‘gates’: a dictionary where keys are gate types and values are counts. - ‘measurements’: a string or dictionary detailing the measurement setup.

Return type:

dict

Raises:

None

property depth

Calculate the depth of the quantum circuit.

The depth of a quantum circuit is defined as the maximum number of gates on any single qubit path in the circuit. This is a measure of the circuit’s complexity and can be used to analyze the circuit’s execution time on a quantum computer.

Returns:

The depth of the quantum circuit, which is the longest path of sequential gate operations on a single qubit.

Return type:

int

Notes

The measurement is not counted when calculating the depth of the circuit.

make_operation_qasm()[source]

Convert an OriginIR circuit description into an OpenQASM string.

This function translates the supported OriginIR gates into their equivalent OpenQASM representations. Unsupported gates are either omitted or transformed into a supported sequence of OpenQASM operations.

Supported OriginIR gates and their OpenQASM equivalents:

  • ‘H’: Hadamard gate

  • ‘X’: Pauli-X gate

  • ‘SX’: Sqrt-X gate (no direct support in OpenQASM)

  • ‘Y’: Pauli-Y gate

  • ‘Z’: Pauli-Z gate

  • ‘CZ’: Controlled-Z gate

  • ‘ISWAP’: iSWAP gate (no direct support in OpenQASM)

  • ‘XY’: XY gate (no direct support in OpenQASM)

  • ‘CNOT’: Controlled NOT gate

  • ‘RX’: Rotation around X-axis

  • ‘RY’: Rotation around Y-axis

  • ‘RZ’: Rotation around Z-axis

  • ‘Rphi’: Two-parameter single-qubit rotation (represented with u3 in OpenQASM)

The conversion process ensures that:

  1. Any unsupported OriginIR operation is either mapped to a supported OpenQASM equivalent or is constructed from OpenQASM primitives. Example: The ‘ISWAP’ gate can be defined in OpenQASM using a sequence of simpler gates.

  2. Gates with the same name in both OriginIR and OpenQASM have identical effects. Any discrepancies, such as a phase factor difference in ‘RZ’, are corrected using OpenQASM’s u3, u2, or u1 gates.

Returns:

The OpenQASM representation of the quantum circuit.

Return type:

str

Notes

The SX, ISWAP, and XY gates are not natively supported in OpenQASM and require a decomposed implementation using native OpenQASM gates.

Examples

>>> originir_circuit = OriginIRCircuit()
>>> print(originir_circuit.qasm)
'...OpenQASM representation...'
Raises:

NotImplementedError – If the conversion functionality is not implemented, an error is raised.

property qasm

Convert the OriginIR representation of the circuit into OpenQASM format.

This property assembles the QASM representation by concatenating the headers, operations, and measurement sections. OpenQASM format has specific syntactical rules: statements are terminated with semicolons, whitespace is non-significant, the language is case-sensitive, and comments start with double forward slashes and end at the end of the line. For user-defined gates, the ‘opaque’ keyword should be used in QASM.

Returns:

The OpenQASM2 representation of the circuit.

Return type:

str

Notes

Currently, this property only supports conversion to OpenQASM2. Future versions may include support for other versions or variants of QASM.

Examples

>>> circuit = QuantumCircuit()
>>> print(circuit.qasm)
'...OpenQASM2 representation...'
Raises:

NotImplementedError – If QASM support is not yet implemented, an error is raised.

unwrap()[source]

Process the given list of OriginIR operations and performs the ‘unwrap’ operation to simplify control structures.

Parameters:

originir (list of str) – A list of strings representing OriginIR operations.

Returns:

A simplified list of OriginIR operations where control structures have been unwrapped. For example, given the input:

QINIT 2
CREG 2
H q[0]
CONTROL q[0]
X q[1]
ENDCONTROL q[0]

The return will be: [“H q[0]”, “X q[1] controlled q[0]”].

Return type:

list of str

Notes

The format for control structure strings is subject to change. The string “X q[1] controlled q[0]” is currently a placeholder.

Raises:

None

class qpandalite.circuit_builder.qcircuit.CircuitControlContext(c, control_list)[source]

(test)Definition of quantum circuit (Circuit).

Class Circuit acts as the OriginIR generator and analysis tool supported by origin_line_parser and origin_base_parser. Each function within the class provides the necessary components to construct quantum circuits. After parsing, each line is transferred to either the OriginIR_Simulator, OriginIR_Parser, OpenQASM2_Parser, or actual quantum machines for execution or further processing.

Module contents