core.circuit

Contents

core.circuit#

Quantum Circuit Package

This package provides tools for creating and manipulating quantum circuits, including parameter expressions and various quantum gates.

It includes: - Parameter expressions for defining parameterized quantum operations - Quantum gate definitions (single-qubit, two-qubit, three-qubit, and parametric gates) - Quantum circuit class for building and manipulating quantum circuits

Usage:

from qamomile.core.circuit import QuantumCircuit, Parameter qc = QuantumCircuit(2) theta = Parameter(‘theta’) qc.rx(theta, 0) qc.cnot(0, 1)

Submodules#

Classes#

Parameter

Represents a named parameter in a quantum circuit.

ParameterExpression

Abstract base class for parameter expressions in quantum circuits.

BinaryOperator

Represents a binary operation between two ParameterExpressions.

Value

Represents a constant numeric value in an expression.

BinaryOpeKind

Enumeration of binary operation types.

QuantumCircuit

Quantum circuit class.

Gate

Abstract base class for all quantum gates.

SingleQubitGate

Unparameterized single qubit gate class.

ParametricSingleQubitGate

Parameterized single qubit gate class.

TwoQubitGate

Two qubit gate class.

ThreeQubitGate

Three qubit gate class.

Operator

Represents a sub-circuit that can be added as a gate.

SingleQubitGateType

Enum class for single qubit gates.

ParametricSingleQubitGateType

Enum class for parametric single qubit gates.

TwoQubitGateType

Enum class for two qubit gates.

ThreeQubitGateType

Enum class for three qubit gates.

ParametricTwoQubitGate

Parameterized two qubit gate class.

ParametricTwoQubitGateType

Enum class for parametric two qubit gates.

MeasurementGate

Measurement gate class.

Package Contents#

class Parameter(name: str)#

Bases: ParameterExpression

Represents a named parameter in a quantum circuit.

Initialize a Parameter with a name.

Parameters:

name (str) – The name of the parameter.

name: str#
get_parameters() list[Parameter]#

Return this parameter in a list.

class ParameterExpression#

Bases: abc.ABC

Abstract base class for parameter expressions in quantum circuits.

This class defines the basic operations (addition, multiplication, division) that can be performed on parameter expressions.

get_parameters() list[Parameter]#

Get the parameters in the expression.

Returns:

The parameters in the expression.

Return type:

list[Parameter]

class BinaryOperator(left, right, kind)#

Bases: ParameterExpression

Represents a binary operation between two ParameterExpressions.

Initialize a BinaryOperator.

Parameters:
left: ParameterExpression#
right: ParameterExpression#
kind: BinaryOpeKind#
get_parameters() list[Parameter]#

Get all parameters involved in this binary operation.

Returns:

A list of all parameters in the expression.

Return type:

list[Parameter]

class Value(value)#

Bases: ParameterExpression

Represents a constant numeric value in an expression.

Initialize a Value with a numeric constant.

Parameters:

value (number) – The constant value.

value#
class BinaryOpeKind#

Bases: enum.Enum

Enumeration of binary operation types.

ADD = '+'#
MUL = '*'#
DIV = '/'#
class QuantumCircuit(num_qubits: int, num_clbits: int = 0, name: str | None = None)#

Quantum circuit class.

This class represents a quantum circuit and provides methods to add various quantum gates and operators to the circuit.

Initialize a quantum circuit with a specified number of qubits.

Parameters:

num_qubits (int) – The number of qubits in the circuit.

gates: list[Gate] = []#
num_qubits#
num_clbits = 0#
name = None#
update_qubits_label(qubits_label: dict[int, str])#

Update the qubits label.

Parameters:

qubits_label (dict[int, str]) – A dictionary of qubit index and its label.

property qubits_label: list[str]#
add_gate(gate: Gate)#

Add a gate to the quantum circuit.

This method checks if the gate’s qubit indices are valid before adding it to the circuit.

Parameters:

gate (Gate) – A gate to be added.

Raises:

ValueError – If the gate’s qubit indices are invalid.

x(index: int)#

Add a Pauli X gate to the quantum circuit.

y(index: int)#

Add a Pauli Y gate to the quantum circuit.

z(index: int)#

Add a Pauli Z gate to the quantum circuit.

h(index: int)#

Add a Hadamard gate to the quantum circuit.

s(index: int)#

Add an S gate to the quantum circuit.

t(index: int)#

Add a T gate to the quantum circuit.

rx(angle: core.circuit.parameter.ParameterExpression, index: int)#

Add a parametric RX gate to the quantum circuit.

\[\begin{split}RX(\theta) = \exp\left(-i\theta X/2\right) = \begin{bmatrix} \cos(\theta/2) & -i\sin(\theta/2) \\ -i\sin(\theta/2) & \cos(\theta/2) \end{bmatrix}\end{split}\]
Parameters:
  • angle (ParameterExpression/float) – The angle parameter for the gate.

  • index (int) – The index of the qubit to apply the gate.

ry(angle: core.circuit.parameter.ParameterExpression, index: int)#

Add a parametric RY gate to the quantum circuit.

\[\begin{split}RY(\theta) = \exp\left(-i\theta Y/2\right) = \begin{bmatrix} \cos(\theta/2) & -\sin(\theta/2) \\ \sin(\theta/2) & \cos(\theta/2) \end{bmatrix}\end{split}\]
Parameters:
  • angle (ParameterExpression/float) – The angle parameter for the gate.

  • index (int) – The index of the qubit to apply the gate.

rz(angle: core.circuit.parameter.ParameterExpression, index: int)#

Add a parametric RZ gate to the quantum circuit.

\[\begin{split}RZ(\theta) = \exp\left(-i\theta Z/2\right) = \begin{bmatrix} e^{-i\theta/2} & 0 \\ 0 & e^{i\theta/2} \end{bmatrix}\end{split}\]
Parameters:
  • angle (ParameterExpression/float) – The angle parameter for the gate.

  • index (int) – The index of the qubit to apply the gate.

cnot(controled_qubit: int, target_qubit: int)#

Add a CNOT gate to the quantum circuit.

cx(controled_qubit: int, target_qubit: int)#

Add a CNOT gate to the quantum circuit.

cz(controled_qubit: int, target_qubit: int)#

Add a CZ gate to the quantum circuit.

crx(angle: core.circuit.parameter.ParameterExpression, controled_qubit: int, target_qubit: int)#

Add a CRX gate to the quantum circuit.

cry(angle: core.circuit.parameter.ParameterExpression, controled_qubit: int, target_qubit: int)#

Add a CRY gate to the quantum circuit.

crz(angle: core.circuit.parameter.ParameterExpression, controled_qubit: int, target_qubit: int)#

Add a CRZ gate to the quantum circuit.

rxx(angle: core.circuit.parameter.ParameterExpression, qubit1: int, qubit2: int)#

Add a RXX gate to the quantum circuit.

\[R_{XX}(\theta) = \exp\left(-i\theta X\otimes X/2\right)\]
ryy(angle: core.circuit.parameter.ParameterExpression, qubit1: int, qubit2: int)#

Add a RYY gate to the quantum circuit.

\[R_{YY}(\theta) = \exp\left(-i\theta Y\otimes Y/2\right)\]
rzz(angle: core.circuit.parameter.ParameterExpression, qubit1: int, qubit2: int)#

Add a RZZ gate to the quantum circuit.

\[R_{ZZ}(\theta) = \exp\left(-i\theta Z\otimes Z/2\right)\]
ccx(control1: int, control2: int, target: int)#

Add a Toffoli gate to the quantum circuit.

exp_evolution(time: core.circuit.parameter.ParameterExpression, hamiltonian: qamomile.core.operator.Hamiltonian)#

Add a parametric exponential gate to the quantum circuit. This function evolves a quantum state under the influence of a Hamiltonian, H, for a given time duration or parameter, t.

The time evolution operator for this gate is given by: .. math:

e^{-it H}
measure(qubit: int, cbit: int)#

Add a measurement gate to the quantum circuit.

Parameters:
  • qubit (int) – The index of the qubit to be measured.

  • cbit (int) – The index of the classical bit to store the measurement result.

measure_all()#

Add measurement gates for all qubits.

append(gate: Gate | QuantumCircuit)#

Append another quantum circuit to this quantum circuit.

Parameters:

qc (QuantumCircuit) – The quantum circuit to be appended.

to_gate(label: str | None = None) Operator#

Convert the quantum circuit to an operator (sub-circuit).

Parameters:

label (str) – The label for the operator.

Returns:

The operator representing the quantum circuit.

Return type:

Operator

get_parameters() set[core.circuit.parameter.Parameter]#

Get the parameters in the quantum circuit.

Returns:

The unique set of parameters in the quantum circuit.

Return type:

set[Parameter]

class Gate#

Bases: abc.ABC

Abstract base class for all quantum gates.

class SingleQubitGate#

Bases: Gate

Unparameterized single qubit gate class.

gate: SingleQubitGateType#
qubit: int#
class ParametricSingleQubitGate#

Bases: Gate

Parameterized single qubit gate class.

gate: ParametricSingleQubitGateType#
qubit: int#
parameter: core.circuit.parameter.ParameterExpression#
class TwoQubitGate#

Bases: Gate

Two qubit gate class.

gate: TwoQubitGateType#
control: int#
target: int#
class ThreeQubitGate#

Bases: Gate

Three qubit gate class.

gate: ThreeQubitGateType#
control1: int#
control2: int#
target: int#
class Operator(circuit: QuantumCircuit, label: str | None = None)#

Bases: Gate

Represents a sub-circuit that can be added as a gate.

circuit#
label = None#
operated_qubits() list[int]#
class SingleQubitGateType#

Bases: enum.Enum

Enum class for single qubit gates.

H = 0#
X = 1#
Y = 2#
Z = 3#
S = 4#
T = 5#
class ParametricSingleQubitGateType#

Bases: enum.Enum

Enum class for parametric single qubit gates.

RX = 0#
RY = 1#
RZ = 2#
class TwoQubitGateType#

Bases: enum.Enum

Enum class for two qubit gates.

CNOT = 0#
CZ = 1#
class ThreeQubitGateType#

Bases: enum.Enum

Enum class for three qubit gates.

CCX = 0#
class ParametricTwoQubitGate#

Bases: Gate

Parameterized two qubit gate class.

gate: ParametricTwoQubitGateType#
control: int#
target: int#
parameter: core.circuit.parameter.ParameterExpression#
class ParametricTwoQubitGateType#

Bases: enum.Enum

Enum class for parametric two qubit gates.

CRX = 0#
CRY = 1#
CRZ = 2#
RXX = 3#
RYY = 4#
RZZ = 5#
class MeasurementGate#

Bases: Gate

Measurement gate class.

qubit: int#
cbit: int#