circuit

Classes representing quantum circuits as a sequence of gates (Operator instances).

Circuits that differ only by permutations of commuting operators are not considered equal.

class parityos.operators.circuit.CircuitLike

Bases: OperatorLike[QubitT_co], Parameterized, HasFrozenQubits[QubitT_co], HasFrozenCBits, Sequence[Operator[QubitT_co]], ABC

Interface for classes representing quantum circuits as ordered sequences of Operator instances.

This class does not apply any operator ordering.

Caution

Equivalent circuits (e.g. circuits that only differ by permutations of commuting operators), are not considered equal.

Example

>>> c1 = Circuit([X(get_q(1)), X(get_q(0)), CNOT(get_q(0), get_q(1))])
>>> c2 = Circuit([X(get_q(0)), X(get_q(1)), CNOT(get_q(0), get_q(1))])
>>> assert c1 != c2
abstract property operators: tuple[Operator[QubitT_co], ...]

Ordered sequence of successive operators which make up the circuit.

__add__(other: Circuit[OtherQubitT_co]) Circuit[QubitT_co | OtherQubitT_co]
__add__(other: CircuitLike[OtherQubitT_co]) FrozenCircuit[QubitT_co | OtherQubitT_co]
__add__(other: FrozenCircuit[OtherQubitT_co]) FrozenCircuit[QubitT_co | OtherQubitT_co]

Addition operator +.

Append another circuit to this circuit. Appending a circuit means appending other’s operators to self’s operators.

Note

If at least one of self or other is a FrozenCircuit, the output is also a FrozenCircuit.

Parameters:

other – Circuit to append.

Returns:

Concatenation of this and the other circuit.

__mul__(amount: int) Self

Multiplication operator *.

Create a new circuit by concatenating this circuit amount number of times. This is similar in spirit to multiplying a list with an integer.

Parameters:

amount – number of times.

Returns:

A copy of self with its operators concatenated amount number of times.

property name: str

Dynamically generated name for this operator.

Where applicable the name must coincide with the OpenQASM standard.

get_hermitian_conjugate() Self

Return the hermitian conjugate (\(\dagger\)) of this operator.

property is_hermitian: bool

Whether this operator is hermitian.

property parameters: frozenset[Parameter]

The symbolic parameters parameterizing this object.

substitute_parameters(old_to_new: Mapping[Parameter, Symbolic | complex]) Self

Substitute symbolic parameters with new symbolic expressions or numeric values.

Parameters:

old_to_new – Mapping from existing parameter symbols to new values (numeric or symbolic).

Returns:

Copy of self with replaced parameters.

__len__() int

The number of operators in this circuit.

__getitem__(index: int) Operator[QubitT_co]
__getitem__(index: slice) tuple[Operator[QubitT_co], ...]

Return a single operator or a range of operators from this circuit.

Operators are indexed by their order in the circuit.

Parameters:

index – The index of a single operator or a slice of indices for a range of operators.

Returns:

The operators at position index in this circuit.

class parityos.operators.circuit.Circuit(operator_sequence: T | Iterable[T] = NOTHING)

Bases: CircuitLike[QubitT_co]

Mutable circuit, which can be built up by successively adding operators.

A mutable Circuit can be turned into an immutable FrozenCircuit by calling freeze.

operator_sequence: list[Operator[QubitT_co]]

Mutable list of successive operators which make up the circuit.

property operators: tuple[Operator[QubitT_co], ...]

Ordered sequence of successive operators which make up the circuit.

property qubits: frozenset[QubitT_co]

Unordered set of qubits this object is defined on.

property ordered_qubits: tuple[QubitT_co, ...]

Ordered sequence of qubits this object is defined on.

property cbits: frozenset[Cbit]

An unordered set of classical bits this object is defined on.

property ordered_cbits: tuple[Cbit, ...]

An ordered sequence of classical bits this object is defined on.

__setitem__(index: int, item: Operator[QubitT_co]) None
__setitem__(index: slice, item: Iterable[Operator[QubitT_co]]) None

Set a single operator or a range of operators.

Operators are indexed by their order in the circuit.

Parameters:
  • index – The index or slice of operators to be set.

  • item – The new Operator instance(s) to be set.

__delitem__(index: int | slice) None

Delete a single operator or a range of operators.

Operators are indexed by their order in the circuit.

Parameters:

index – the index or slice of operators to be deleted

append(value: Operator[QubitT_co]) None

Append an operator to the end of the circuit.

insert(index: int, value: Operator[QubitT_co]) None

Insert an operator into the circuit between index-1 and index.

Parameters:
  • index – Where the new operator should be inserted.

  • value – The new operator to be inserted.

measure_all() Self

Add Z measurement operations (MZ) for all qubits in the circuit.

A new classical bit is created and added to the circuit for each qubit in the circuit.

Note

The added classical bits are enumerated in the order of ordered_qubits with increasing ids, skipping any ids already taken by existing classical bits.

Returns:

Modified self with measurements for all circuit qubits appended.

freeze() FrozenCircuit[QubitT_co]

Return an immutable copy of this circuit.

class parityos.operators.circuit.FrozenCircuit(operator_sequence: T | Iterable[T])

Bases: CircuitLike[QubitT_co], HasFrozenUnorderedQubits[QubitT_co], HasFrozenUnorderedCBits

Immutable Circuit, which is also hashable.

No operators can be added or removed. An immutable FrozenCircuit can be turned into a mutable Circuit by calling unfreeze.

operator_sequence: tuple[Operator[QubitT_co], ...]

Immutable tuple of successive operators which make up the circuit.

property operators: tuple[Operator[QubitT_co], ...]

Ordered sequence of successive operators which make up the circuit.

qubits: frozenset[QubitT_co]

The set of all qubits this circuit acts on.

cbits: frozenset[Cbit]

The set of all classical bits this circuit acts on.

unfreeze() Circuit[QubitT_co]

Return a mutable copy of this circuit.