graphix.transpiler module

Gate-to-MBQC transpiler.

accepts desired gate operations and transpile into MBQC measurement patterns.

class TranspiledPattern(pattern: Pattern, classical_outputs: tuple[Node, ...])[source]

Bases: object

A transpiled pattern.

pattern: Pattern
classical_outputs: tuple[Node, ...]

Nodes measured with circuit measurements, in the order of the gates.

__init__(pattern: Pattern, classical_outputs: tuple[Node, ...]) → None
class TranspiledFlow(flow: CausalFlow[BlochMeasurement], classical_outputs: dict[int, M])[source]

Bases: object

A transpiled causal flow.

flow: CausalFlow[BlochMeasurement]
classical_outputs: dict[int, M]

M commands for nodes measured with circuit measurements.

to_pattern() → TranspiledPattern[source]

Return the transpiled pattern.

__init__(flow: CausalFlow[BlochMeasurement], classical_outputs: dict[int, M]) → None
class SimulateResult(state: _DenseStateT, classical_measures: tuple[int, ...])[source]

Bases: Generic[_DenseStateT]

Result of a circuit simulation.

state_DenseStateT

State representation of the simulation output.

classical_measurestuple[int,…]

Results of classical measurements.

state: _DenseStateT
classical_measures: tuple[int, ...]
__init__(state: _DenseStateT, classical_measures: tuple[int, ...]) → None
class Circuit(width: int, instr: Iterable[InstructionType] | None = None)[source]

Bases: InplaceParameterizable

Gate-to-MBQC transpiler.

Holds gate operations and translates into MBQC measurement patterns.

width

Number of logical qubits (for gate network)

Type:

int

instruction

List containing the gate sequence applied.

Type:

list

__init__(width: int, instr: Iterable[InstructionType] | None = None) → None[source]

Construct a circuit.

Parameters:
  • width (int) – number of logical qubits for the gate network

  • instr (list[InstructionType] | None) – Optional. List of initial instructions.

instruction: list[InstructionType]
add(instr: InstructionType) → None[source]

Add an instruction to the circuit.

extend(instrs: Iterable[InstructionType]) → None[source]

Add instructions to the circuit.

cnot(control: int, target: int) → None[source]

Apply a CNOT gate.

See CNOT for more information.

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

swap(qubit1: int, qubit2: int) → None[source]

Apply a SWAP gate.

See SWAP for more information.

Parameters:
  • qubit1 (int) – first qubit to be swapped

  • qubit2 (int) – second qubit to be swapped

cz(qubit1: int, qubit2: int) → None[source]

Apply a CZ gate.

See CZ for more information.

Parameters:
  • qubit1 (int) – control qubit

  • qubit2 (int) – target qubit

h(qubit: int) → None[source]

Apply a Hadamard gate.

See H for more information.

Parameters:

qubit (int) – target qubit

s(qubit: int) → None[source]

Apply an S gate.

See S for more information.

Parameters:

qubit (int) – target qubit

x(qubit: int) → None[source]

Apply a Pauli X gate.

See X for more information.

Parameters:

qubit (int) – target qubit

y(qubit: int) → None[source]

Apply a Pauli Y gate.

See Y for more information.

Parameters:

qubit (int) – target qubit

z(qubit: int) → None[source]

Apply a Pauli Z gate.

See Z for more information.

Parameters:

qubit (int) – target qubit

rx(qubit: int, angle: ParameterizedAngle) → None[source]

Apply an X rotation gate.

See RX for more information.

Parameters:
  • qubit (int) – target qubit

  • angle (ParameterizedAngle) – rotation angle in units of π

ry(qubit: int, angle: ParameterizedAngle) → None[source]

Apply a Y rotation gate.

See RY for more information.

Parameters:
  • qubit (int) – target qubit

  • angle (ParameterizedAngle) – angle in units of π

rz(qubit: int, angle: ParameterizedAngle) → None[source]

Apply a Z rotation gate.

See RZ for more information.

Parameters:
  • qubit (int) – target qubit

  • angle (ParameterizedAngle) – rotation angle in units of π

j(qubit: int, angle: ParameterizedAngle) → None[source]

Apply a J rotation gate.

See J for more information.

Parameters:
  • qubit (int) – target qubit

  • angle (ParameterizedAngle) – rotation angle in units of π

r(qubit: int, axis: Axis, angle: ParameterizedAngle) → None[source]

Apply a rotation gate on the given axis.

Parameters:
  • qubit (int) – target qubit

  • axis (Axis) – rotation axis

  • angle (ParameterizedAngle) – rotation angle in units of π

rzz(control: int, target: int, angle: ParameterizedAngle) → None[source]

Apply a ZZ-rotation gate.

Equivalent to the sequence CNOT(control, target), Rz(target, angle), CNOT(control, target)

and realizes rotation expressed by \(e^{-i \frac{\theta}{2} Z_c Z_t}\).

See RZZ for more information.

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

  • angle (ParameterizedAngle) – rotation angle in units of π

ccx(control1: int, control2: int, target: int) → None[source]

Apply a CCX (Toffoli) gate.

See CCX for more information.

Parameters:
  • control1 (int) – first control qubit

  • control2 (int) – second control qubit

  • target (int) – target qubit

i(qubit: int) → None[source]

Apply an identity (teleportation) gate.

See I for more information.

Parameters:

qubit (int) – target qubit

m(qubit: int, axis: Axis) → None[source]

Measure a quantum qubit.

The measured qubit cannot be used afterwards.

See M for more information.

Parameters:
  • qubit (int) – target qubit

  • axis (Axis) – measurement basis

sdg(qubit: int) → None[source]

Apply an SDG gate.

See SDG for more information.

Parameters:

qubit (int) – target qubit

t(qubit: int) → None[source]

Apply a T gate.

See T for more information.

Parameters:

qubit (int) – target qubit

tdg(qubit: int) → None[source]

Apply a TDG gate.

See TDG for more information.

Parameters:

qubit (int) – target qubit

sx(qubit: int) → None[source]

Apply an SX gate.

See SX for more information.

Parameters:

qubit (int) – target qubit

sxdg(qubit: int) → None[source]

Apply an SXDG gate.

See SXDG for more information.

Parameters:

qubit (int) – target qubit

cy(control: int, target: int) → None[source]

Apply a Controlled-Y gate.

See CY for more information.

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

p(qubit: int, angle: ParameterizedAngle) → None[source]

Apply a Phase rotation gate.

See P for more information.

Parameters:
  • qubit (int) – target qubit

  • angle (ParameterizedAngle) – rotation angle in units of π

u(qubit: int, theta: ParameterizedAngle, phi: ParameterizedAngle, lambda_: ParameterizedAngle) → None[source]

Apply a universal single-qubit gate.

See U for more information.

Parameters:
  • qubit (int) – target qubit

  • theta (ParameterizedAngle) – rotation angle in units of π

  • phi (ParameterizedAngle) – rotation angle in units of π

  • lambda (ParameterizedAngle) – rotation angle in units of π

cj(control: int, target: int, angle: ParameterizedAngle) → None[source]

Apply a controlled-J rotation gate.

See CJ for more information.

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

  • angle (ParameterizedAngle) – rotation angle in units of π

cp(control: int, target: int, angle: ParameterizedAngle) → None[source]

Apply a controlled-P rotation gate.

See CP for more information.

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

  • angle (ParameterizedAngle) – rotation angle in units of π

crx(control: int, target: int, angle: ParameterizedAngle) → None[source]

Apply an controlled-X rotation gate.

See CRX for more information.

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

  • angle (ParameterizedAngle) – rotation angle in units of π

cry(control: int, target: int, angle: ParameterizedAngle) → None[source]

Apply a controlled-Y rotation gate.

See CRY for more information.

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

  • angle (ParameterizedAngle) – angle in units of π

crz(control: int, target: int, angle: ParameterizedAngle) → None[source]

Apply a controlled-Z rotation gate.

See CRZ for more information.

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

  • angle (ParameterizedAngle) – rotation angle in units of π

cr(control: int, target: int, axis: Axis, angle: ParameterizedAngle) → None[source]

Apply a controlled-rotation gate on the given axis.

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

  • axis (Axis) – rotation axis

  • angle (ParameterizedAngle) – rotation angle in units of π

cu(control: int, target: int, theta: ParameterizedAngle, phi: ParameterizedAngle, lambda_: ParameterizedAngle, gamma: ParameterizedAngle) → None[source]

Apply a controlled-U gate.

See CU for more information.

Parameters:
  • control (int) – control qubit

  • target (int) – target qubit

  • theta (ParameterizedAngle) – rotation angle in units of π

  • phi (ParameterizedAngle) – rotation angle in units of π

  • lambda (ParameterizedAngle) – rotation angle in units of π

  • gamma (ParameterizedAngle) – rotation angle in units of π

cswap(control: int, qubit1: int, qubit2: int) → None[source]

Apply a CSWAP gate.

See CSWAP for more information.

Parameters:
  • control (int) – control qubit

  • qubit1 (int) – first qubit to be swapped

  • qubit2 (int) – second qubit to be swapped

gphase(angle: ParameterizedAngle) → None[source]

Apply a global phase.

See GPHASE for more information.

Parameters:

angle (ParameterizedAngle) – rotation angle in units of π

transpile_to_causalflow() → TranspiledFlow[source]

Transpile a circuit via J-∧z decomposition to a causal flow.

Parameters:

self (the circuit to transpile.)

Returns:

the result of the transpilation

Return type:

a causal flow and classical outputs.

transpile(*, transpile_swaps: bool = True) → TranspiledPattern[source]

Transpile a circuit via J-∧z decomposition to a pattern.

Parameters:

transpile_swaps (bool, optional) – If True (the default), SWAP gates are eliminated by switching the qubits. If False, SWAP gates are transpiled into a sequence of three CNOT gates.

Returns:

The result of the transpilation: a pattern and classical outputs.

Return type:

TranspiledPattern

simulate(backend: StatevectorBackend | Literal['statevector'] = 'statevector', input_state: Data | None = None, branch_selector: BranchSelector | None = None, rng: Generator | None = None, *, stacklevel: int = 1) → SimulateResult[Statevector][source]
simulate(backend: DensityMatrixBackend | Literal['densitymatrix'], input_state: Data | None = None, branch_selector: BranchSelector | None = None, rng: Generator | None = None, *, stacklevel: int = 1) → SimulateResult[DensityMatrix]
simulate(backend: DenseStateBackend[_DenseStateT], input_state: Data | None = None, branch_selector: BranchSelector | None = None, rng: Generator | None = None, *, stacklevel: int = 1) → SimulateResult[_DenseStateT]

Simulate the gate sequence with a backend and input state of choice.

By default, this method uses the statevector backend and initializes the register to \(|+\rangle^{\otimes n}\).

Parameters:
  • input_state (Data)

  • backend (graphix.sim.base_backend.DenseStateBackend[_DenseStateT], ‘statevector’, or ‘densitymatrix’) – Simulator backend to use. Optional, defaults to “statevector”.

  • branch_selector (graphix.branch_selector.BranchSelector) – branch selector for measures (default: RandomBranchSelector). It cannot be specified if backend is already instantiated.

  • rng (Generator, optional) – Random-number generator for measurements. This generator is used only in case of random branch selection (see RandomBranchSelector).

Returns:

result – output state of the statevector simulation and results of classical measures.

Return type:

SimulateResult

visit(visitor: InstructionVisitor, *, copy: bool = False) → Circuit[source]

Apply visitor to all instructions in the circuit.

Parameters:
  • visitor (InstructionVisitor) – The visitor specifying the rewriting.

  • copy (bool, optional) – If True, the current circuit remains unchanged, and a new circuit is returned. The default is False, meaning that changes are performed in place.

Returns:

The rewritten circuit. Equal to self if copy is False.

Return type:

Self

apply_angle(f: Callable[[ParameterizedAngle], ParameterizedAngle], *, copy: bool = False) → Circuit[source]

Apply f to all angles that occur in the circuit.

Parameters:
  • f (Callable[[ParameterizedAngle], ParameterizedAngle]) – The function to apply to every angle.

  • copy (bool, optional) – If True, the current circuit remains unchanged, and a new circuit is returned. The default is False, meaning that changes are performed in place.

Returns:

The rewritten circuit. Equal to self if copy is False.

Return type:

Self

is_parameterized() → bool[source]

Return True if there is at least one measurement angle that is not just an instance of SupportsFloat.

A parameterized circuit is a circuit where at least one measurement angle is an expression that is not a number, typically an instance of sympy.Expr (but we don’t force to choose sympy here).

replace_parameter(variable: Parameter, substitute: ExpressionOrSupportsFloat, *, copy: bool = False) → Circuit[source]

Substitute all occurrences of the given variable in measurement angles by the given value.

Parameters:
  • variable (Parameter) – The symbolic expression to be replaced within the measurement angles.

  • substitute (ExpressionOrSupportsFloat) – The value or symbolic expression to substitute in place of variable.

  • copy (bool, optional) – If True, the current object remains unchanged and a new object is returned. The default is False, meaning that changes are performed in place.

Returns:

The object with the updated measurement parameters. Equal to self if copy is False.

Return type:

Self

replace_parameters(assignment: Mapping[Parameter, ExpressionOrSupportsFloat], *, copy: bool = False) → Circuit[source]

Substitute all occurrences of the given keys in measurement angles by the given values in parallel.

Parameters:
  • assignment (Mapping[Parameter, ExpressionOrSupportsFloat]) – A dictionary-like mapping where keys are the Parameter objects to be replaced and values are the new expressions or numerical values.

  • copy (bool, optional) – If True, the current object remains unchanged and a new object is returned. The default is False, meaning that changes are performed in place.

Returns:

The object with the updated measurement parameters. Equal to self if copy is False.

Return type:

Self

transpile_to_qasm_gates() → Circuit[source]

Return an equivalent circuit using only the standard OpenQASM gate set.

transpile_rzz() → Circuit[source]

Return an equivalent circuit where all RZZ gates have been replaced with OpenQASM gates.

decompose_rzz(instr: Instruction.RZZ) → Iterator[Instruction.CNOT | Instruction.RZ][source]

Yield a decomposition of RZZ(α) gate as CNOT(control, target)·Rz(target, α)·CNOT(control, target).

Parameters:

instr (the RZZ instruction to decompose.)

Return type:

the decomposition.

decompose_ccx(instr: Instruction.CCX) → Iterator[Instruction.H | Instruction.CNOT | Instruction.RZ][source]

Yield a decomposition of the CCX gate into H, CNOT, T and T-dagger gates.

This decomposition of the Toffoli gate can be found in Michael A. Nielsen and Isaac L. Chuang, Quantum Computation and Quantum Information, Cambridge University Press, 2000 (p. 182 in the 10th Anniversary Edition).

Parameters:

instr (the CCX instruction to decompose.)

Return type:

the decomposition.

decompose_cnot(instr: Instruction.CNOT) → Iterator[Instruction.H | Instruction.CZ][source]

Yield a decomposition of the CNOT gate as H·∧z·H.

Vincent Danos, Elham Kashefi, Prakash Panangaden, The Measurement Calculus, 2007.

Parameters:

instr (the CNOT instruction to decompose.)

Return type:

the decomposition.

decompose_swap(instr: Instruction.SWAP) → Iterator[Instruction.CNOT][source]

Yield a decomposition of the SWAP gate as CNOT(0, 1)·CNOT(1, 0)·CNOT(0, 1).

Michael A. Nielsen and Isaac L. Chuang, Quantum Computation and Quantum Information, Cambridge University Press, 2000 (p. 23 in the 10th Anniversary Edition).

Parameters:

instr (the SWAP instruction to decompose.)

Return type:

the decomposition.

decompose_y(instr: Instruction.Y) → Iterator[Instruction.X | Instruction.Z | Instruction.GPHASE][source]

Return a decomposition of the Y gate as \(\mathrm e^{\mathrm i \frac \pi 2} X Z\).

Parameters:

instr (the Y instruction to decompose.)

Return type:

the decomposition.

decompose_rx(instr: Instruction.RX) → Iterator[Instruction.J | Instruction.GPHASE][source]

Yield a J decomposition of the RX gate.

The Rx(α) gate is decomposed into J(α)·H (that is to say, J(α)·J(0)). Vincent Danos, Elham Kashefi, Prakash Panangaden, The Measurement Calculus, 2007.

Parameters:

instr (the RX instruction to decompose.)

Return type:

the decomposition.

decompose_ry(instr: Instruction.RY) → Iterator[Instruction.J | Instruction.GPHASE][source]

Yield a J decomposition of the RY gate.

The Ry(α) gate is decomposed into J(0)·J(π/2)·J(α)·J(-π/2). Vincent Danos, Elham Kashefi, Prakash Panangaden, Robust and parsimonious realisations of unitaries in the one-way model, 2004.

Parameters:

instr (the RY instruction to decompose.)

Return type:

the decomposition.

decompose_rz(instr: Instruction.RZ) → Iterator[Instruction.J | Instruction.GPHASE][source]

Yield a J decomposition of the RZ gate.

The Rz(α) gate is decomposed into H·J(α) (that is to say, J(0)·J(α)). Vincent Danos, Elham Kashefi, Prakash Panangaden, The Measurement Calculus, 2007.

Parameters:

instr (the RZ instruction to decompose.)

Return type:

the decomposition.

decompose_u(instr: Instruction.U) → Iterator[Instruction.J | Instruction.GPHASE][source]

Yield a J decomposition of the U gate.

The \(U(\theta, \phi, \lambda)\) gate is decomposed as \(e^{-i \theta/2} \cdot H \cdot J(\phi + \pi/2) \cdot J(\theta) \cdot J(\lambda - \pi/2)\), or equivalently, \(J(0) \cdot J(\phi + \pi/2) \cdot J(\theta) \cdot J(\lambda - \pi/2)\).

Parameters:

instr (the U instruction to decompose.)

Return type:

the decomposition.

decompose_cu(instr: Instruction.CU) → Iterator[Instruction.CJ | Instruction.P][source]

Yield a J decomposition of the U gate.

The U(θ, φ, λ) gate is decomposed into H·J(φ + 𝜋/2)·J(θ)·J(λ - 𝜋/2) (that is to say, J(0)·J(φ + 𝜋/2)·J(θ)·J(λ - 𝜋/2)).

Parameters:

instr (the U instruction to decompose.)

Return type:

the decomposition.

insert_control(control: int, instrs: Iterable[Instruction.GPHASE | Instruction.X | Instruction.Z | Instruction.J | Instruction.CNOT | Instruction.RZ]) → Iterator[Instruction.CNOT | Instruction.CZ | Instruction.CJ | Instruction.CCX | Instruction.CRZ | Instruction.P][source]

Yield a controlled gate sequence from a gate sequence.

Parameters:
Yields:

InstructionType – The controlled gate sequence.

decompose_cj(instr: Instruction.CJ) → Iterator[Instruction.RZ | Instruction.CNOT | Instruction.RY | Instruction.P][source]

Yield a decomposed gate sequence of the CJ gate.

See CJ for more information.

decompose_p(instr: Instruction.P) → Iterator[Instruction.RZ | Instruction.GPHASE][source]

Yield a decomposed gate sequence of the P gate.

See P for more information.

instructions_to_jcz(instrs: Iterable[InstructionType]) → Iterator[Instruction.J | Instruction.CZ | Instruction.M | Instruction.GPHASE][source]

Yield a J-∧z decomposition of the instruction.

Parameters:

instr (the instruction to decompose.)

Return type:

the decomposition.

normalize_angle(angle: ParameterizedAngle) → ParameterizedAngle[source]

Return an equivalent angle in range \([0, 2 \cdot \pi)\) if angle is instantiated.

Parameters:

angle (ParameterizedAngle) – An angle.

Returns:

An equivalent angle in range \([0, 2 \cdot \pi)\) if angle is instantiated. If angle is parameterized, angle is returned unchanged.

Return type:

ParameterizedAngle

class TranspileSwapsResult(circuit: Circuit, outputs: tuple[OutputIndex, ...])[source]

Bases: object

The result returned by transpile_swaps().

circuit: Circuit

Circuit without SWAP gates.

outputs: tuple[OutputIndex, ...]

Tuple which has the same width as the circuit and which for every qubit of the original circuit provides the index of the corresponding qubit in the output of the swapped circuit (either measured or not).

extract_outputs(kind: OutputKind) → tuple[int, ...][source]

Return the sequence of outputs of the given kind.

extract_output_node_indices() → tuple[int, ...][source]

Return for each output node, sorted in the order of the original circuit, the index of the corresponding output node in the order of the swapped circuit.

This method returns a permutation of range(number_of_output_qubits).

swap_output_nodes(output_nodes: Sequence[Node]) → tuple[Node, ...][source]

Reorder the output nodes of a pattern obtained from a swapped circuit to restore the qubit ordering of the original circuit.

swap_classical_outputs(classical_outputs: Sequence[Node]) → tuple[int, ...][source]

Reorder the classical outpus of a pattern obtained from a swapped circuit to restore the output ordering of the original circuit.

__init__(circuit: Circuit, outputs: tuple[OutputIndex, ...]) → None
class OutputKind(*values)[source]

Bases: Enum

Specify whether a qubit is measured or not.

Qubit = 1
Bit = 2
class OutputIndex(kind: OutputKind, index: int)[source]

Bases: object

Index of a swapped qubit.

If the qubit is measured, kind equals to OutputKind.Bit and index is the index of the measurement.

If the qubit is not measured, kind equals to OutputKind.qubit and index is the index of the qubit in the swapped circuit.

__init__(kind: OutputKind, index: int) → None
kind: OutputKind
index: int
transpile_swaps(circuit: Circuit, *, copy: bool = False) → TranspileSwapsResult[source]

Return a new circuit equivalent to the original one but without SWAP gates.

Parameters:
  • circuit (Circuit) – The original circuit

  • copy (bool, optional) – If True, the current pattern remains unchanged, and a new pattern is returned. The default is False, meaning that changes are performed in place.

Returns:

The field circuit contains an equivalent circuit without SWAP gates. Equal to self if copy is False.

The field outputs contains a tuple which has the same width as the circuit. For every qubit of the original circuit, either the qubit is not measured, and outputs provides the index of the corresponding qubit in the output of the returned circuit; or the qubit has been measured, and outputs provides the index of the measurement.

Return type:

TranspileSwapsResult