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:
objectA transpiled pattern.
- class TranspiledFlow(flow: CausalFlow[BlochMeasurement], classical_outputs: dict[int, M])[source]¶
Bases:
objectA transpiled causal flow.
- flow: CausalFlow[BlochMeasurement]¶
- 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¶
- class Circuit(width: int, instr: Iterable[InstructionType] | None = None)[source]¶
Bases:
InplaceParameterizableGate-to-MBQC transpiler.
Holds gate operations and translates into MBQC measurement patterns.
- __init__(width: int, instr: Iterable[InstructionType] | None = None) None[source]¶
Construct a circuit.
- h(qubit: int) None[source]¶
Apply a Hadamard gate.
See
Hfor more information.- Parameters:
qubit (int) – target qubit
- s(qubit: int) None[source]¶
Apply an S gate.
See
Sfor more information.- Parameters:
qubit (int) – target qubit
- x(qubit: int) None[source]¶
Apply a Pauli X gate.
See
Xfor more information.- Parameters:
qubit (int) – target qubit
- y(qubit: int) None[source]¶
Apply a Pauli Y gate.
See
Yfor more information.- Parameters:
qubit (int) – target qubit
- z(qubit: int) None[source]¶
Apply a Pauli Z gate.
See
Zfor more information.- Parameters:
qubit (int) – target qubit
- rx(qubit: int, angle: ParameterizedAngle) None[source]¶
Apply an X rotation gate.
See
RXfor 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
RYfor 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
RZfor 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
Jfor 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.
- 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
RZZfor more information.
- ccx(control1: int, control2: int, target: int) None[source]¶
Apply a CCX (Toffoli) gate.
See
CCXfor more information.
- i(qubit: int) None[source]¶
Apply an identity (teleportation) gate.
See
Ifor 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
Mfor more information.
- sdg(qubit: int) None[source]¶
Apply an SDG gate.
See
SDGfor more information.- Parameters:
qubit (int) – target qubit
- t(qubit: int) None[source]¶
Apply a T gate.
See
Tfor more information.- Parameters:
qubit (int) – target qubit
- tdg(qubit: int) None[source]¶
Apply a TDG gate.
See
TDGfor more information.- Parameters:
qubit (int) – target qubit
- sx(qubit: int) None[source]¶
Apply an SX gate.
See
SXfor more information.- Parameters:
qubit (int) – target qubit
- sxdg(qubit: int) None[source]¶
Apply an SXDG gate.
See
SXDGfor more information.- Parameters:
qubit (int) – target qubit
- p(qubit: int, angle: ParameterizedAngle) None[source]¶
Apply a Phase rotation gate.
See
Pfor 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
Ufor 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
CJfor more information.
- cp(control: int, target: int, angle: ParameterizedAngle) None[source]¶
Apply a controlled-P rotation gate.
See
CPfor more information.
- crx(control: int, target: int, angle: ParameterizedAngle) None[source]¶
Apply an controlled-X rotation gate.
See
CRXfor more information.
- cry(control: int, target: int, angle: ParameterizedAngle) None[source]¶
Apply a controlled-Y rotation gate.
See
CRYfor more information.
- crz(control: int, target: int, angle: ParameterizedAngle) None[source]¶
Apply a controlled-Z rotation gate.
See
CRZfor more information.
- cr(control: int, target: int, axis: Axis, angle: ParameterizedAngle) None[source]¶
Apply a controlled-rotation gate on the given axis.
- cu(control: int, target: int, theta: ParameterizedAngle, phi: ParameterizedAngle, lambda_: ParameterizedAngle, gamma: ParameterizedAngle) None[source]¶
Apply a controlled-U gate.
See
CUfor more information.- Parameters:
- cswap(control: int, qubit1: int, qubit2: int) None[source]¶
Apply a CSWAP gate.
See
CSWAPfor more information.
- gphase(angle: ParameterizedAngle) None[source]¶
Apply a global phase.
See
GPHASEfor 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. IfFalse, SWAP gates are transpiled into a sequence of three CNOT gates.- Returns:
The result of the transpilation: a pattern and classical outputs.
- Return type:
- 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 ifbackendis 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:
- visit(visitor: InstructionVisitor, *, copy: bool = False) Circuit[source]¶
Apply
visitorto 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 isFalse, meaning that changes are performed in place.
- Returns:
The rewritten circuit. Equal to
selfifcopyisFalse.- Return type:
Self
- apply_angle(f: Callable[[ParameterizedAngle], ParameterizedAngle], *, copy: bool = False) Circuit[source]¶
Apply
fto 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 isFalse, meaning that changes are performed in place.
- Returns:
The rewritten circuit. Equal to
selfifcopyisFalse.- Return type:
Self
- is_parameterized() bool[source]¶
Return
Trueif there is at least one measurement angle that is not just an instance ofSupportsFloat.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 choosesympyhere).
- 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 isFalse, meaning that changes are performed in place.
- Returns:
The object with the updated measurement parameters. Equal to
selfifcopyisFalse.- 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
Parameterobjects 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 isFalse, meaning that changes are performed in place.
- Returns:
The object with the updated measurement parameters. Equal to
selfifcopyisFalse.- Return type:
Self
- 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:
control (int) – The control qubit.
instrs (Iterable[Instruction.GPHASE | Instruction.X | Instruction.Z | Instruction.J | Instruction.CNOT | Instruction.RZ]) – The gate sequence.
- 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
CJfor more information.
- decompose_p(instr: Instruction.P) Iterator[Instruction.RZ | Instruction.GPHASE][source]¶
Yield a decomposed gate sequence of the P gate.
See
Pfor 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
angleis instantiated.- Parameters:
angle (ParameterizedAngle) – An angle.
- Returns:
An equivalent angle in range \([0, 2 \cdot \pi)\) if
angleis instantiated. Ifangleis parameterized,angleis returned unchanged.- Return type:
ParameterizedAngle
- class TranspileSwapsResult(circuit: Circuit, outputs: tuple[OutputIndex, ...])[source]¶
Bases:
objectThe result returned by
transpile_swaps().- 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:
EnumSpecify whether a qubit is measured or not.
- Qubit = 1¶
- Bit = 2¶
- class OutputIndex(kind: OutputKind, index: int)[source]¶
Bases:
objectIndex of a swapped qubit.
If the qubit is measured,
kindequals toOutputKind.Bitandindexis the index of the measurement.If the qubit is not measured,
kindequals toOutputKind.qubitandindexis the index of the qubit in the swapped circuit.- __init__(kind: OutputKind, index: int) None¶
- kind: OutputKind¶
- transpile_swaps(circuit: Circuit, *, copy: bool = False) TranspileSwapsResult[source]¶
Return a new circuit equivalent to the original one but without SWAP gates.
- Parameters:
- Returns:
The field
circuitcontains an equivalent circuit without SWAP gates. Equal toselfifcopyisFalse.The field
outputscontains a tuple which has the same width as the circuit. For every qubit of the original circuit, either the qubit is not measured, andoutputsprovides the index of the corresponding qubit in the output of the returned circuit; or the qubit has been measured, andoutputsprovides the index of the measurement.- Return type: