# pytket.passes

class pytket.passes.BasePass

Base class for passes.

__init__(*args, **kwargs)
apply(*args, **kwargs)

1. apply(self: pytket.passes.BasePass, compilation_unit: pytket.predicates.CompilationUnit, safety_mode: pytket.passes.SafetyMode = <SafetyMode.Default: 1>) -> bool

Apply to a CompilationUnit.

Returns

True if pass modified the circuit, else False

1. apply(self: pytket.passes.BasePass, circuit: pytket.circuit.Circuit) -> bool

Apply to a Circuit in-place.

Returns

True if pass modified the circuit, else False

1. apply(self: pytket.passes.BasePass, circuit: pytket.circuit.Circuit, before_apply: Callable[[pytket.predicates.CompilationUnit, json], None], after_apply: Callable[[pytket.predicates.CompilationUnit, json], None]) -> bool

Apply to a Circuit in-place and invoke callbacks for all nested passes.

Parameters
• before_apply – Invoked before a pass is applied. The CompilationUnit and a summary of the pass configuration are passed into the callback.

• after_apply – Invoked after a pass is applied. The CompilationUnit and a summary of the pass configuration are passed into the callback.

Returns

True if pass modified the circuit, else False

static from_dict(arg0: json)

Construct a new Pass instance from a JSON serializable dictionary representation.

to_dict(self: pytket.passes.BasePass) json
Returns

A JSON serializable dictionary representation of the Pass.

class pytket.passes.RepeatPass

Repeat a pass until it has no effect.

__init__(self: pytket.passes.RepeatPass, compilation_pass: pytket.passes.BasePass) None

Construct from a compilation pass.

get_pass(self: pytket.passes.RepeatPass)
Returns

The underlying compilation pass.

class pytket.passes.RepeatUntilSatisfiedPass

Repeat a compilation pass until a predicate on the circuit is satisfied.

__init__(*args, **kwargs)

1. __init__(self: pytket.passes.RepeatUntilSatisfiedPass, compilation_pass: pytket.passes.BasePass, predicate: pytket.predicates.Predicate) -> None

Construct from a compilation pass and a predicate.

1. __init__(self: pytket.passes.RepeatUntilSatisfiedPass, compilation_pass: pytket.passes.BasePass, check_function: Callable[[pytket.circuit.Circuit], bool]) -> None

Construct from a compilation pass and a user-defined function from Circuit to bool.

get_pass(self: pytket.passes.RepeatUntilSatisfiedPass)
Returns

The underlying compilation pass.

get_predicate(self: pytket.passes.RepeatUntilSatisfiedPass)
Returns

The underlying predicate.

class pytket.passes.RepeatWithMetricPass

Repeat a compilation pass until the given metric stops decreasing.

__init__(self: pytket.passes.RepeatWithMetricPass, compilation_pass: pytket.passes.BasePass, metric: Callable[[pytket.circuit.Circuit], int]) None

Construct from a compilation pass and a metric function.

get_metric(self: pytket.passes.RepeatWithMetricPass) Callable[[pytket.circuit.Circuit], int]
Returns

The underlying metric.

get_pass(self: pytket.passes.RepeatWithMetricPass)
Returns

The underlying compilation pass.

class pytket.passes.SafetyMode

Members:

Audit : Checks which predicates a circuit satisfies after the application of each base pass

Default : Only check that a circuit satisfies the preconditions of the overall pass at the start and the postconditions at the end

__init__(self: pytket.passes.SafetyMode, value: int) None
property name
class pytket.passes.SequencePass

A sequence of compilation passes.

__init__(self: pytket.passes.SequencePass, pass_list: List[pytket.passes.BasePass]) None

Construct from a list of compilation passes arranged in order of application.

get_sequence(self: pytket.passes.SequencePass) List[pytket.passes.BasePass]
Returns

The underlying sequence of passes.

pytket.passes.AASRouting(arc: pytket.routing.Architecture, **kwargs)

Construct a pass to relabel Circuit Qubits to Device Nodes, and then use architecture-aware synthesis to route the circuit. In the steps of the pass the circuit will be converted to CX, Rz, H gateset. The limited connectivity of the Architecture is used for the routing. The direction of the edges is ignored. The placement used is GraphPlacement. This pass can take a few parameters for the routing, described below.

NB: In the current implementation it is assumed that the number of nodes in the architecture is equal to the number of qubits in the circuit. With smaller circuits may therefore be necessary to add unused qubits before applying this pass.

Parameters
• arc – The architecture used for connectivity information.

• **kwargs – Parameters for routing: (unsigned) lookahead=1: giving parameter for the recursive iteration depth in the synthesis method

Returns

a pass to perform the remapping

pytket.passes.CXMappingPass(arc: pytket.routing.Architecture, placer: pytket.routing.Placement, **kwargs)

Construct a pass to convert all gates to CX, relabel Circuit Qubits to :py:class’Architecture’ Nodes, route to the connectivty graph of a Architecture and decompose additional routing gates (SWAP and BRIDGE) to CX gates.

Parameters
• arc – The Architecture used for connectivity information.

• placer – The placement used for relabelling.

• **kwargs – Parameters for routing: (int)swap_lookahead=50, (int)bridge_lookahead=4, (int)bridge_interactions=2, (float)bridge_exponent=0, (bool)directed_cx=false, (bool)delay_measures=true

Returns

a pass to perform the remapping

pytket.passes.CliffordSimp(allow_swaps: bool = True)

An optimisation pass that performs a number of rewrite rules for simplifying Clifford gate sequences, similar to Duncan & Fagan (https://arxiv.org/abs/1901.10114). Given a circuit with CXs and any single-qubit gates, produces a circuit with tk1, CX gates.

Parameters

allow_swaps – dictates whether the rewriting will disregard CX placement or orientation and introduce wire swaps.

Returns

a pass to perform the rewriting

pytket.passes.CommuteThroughMultis()

Moves single-qubit operations past multi-qubit operations that they commute with, towards the front of the circuit.

pytket.passes.ContextSimp(allow_classical: bool = True, xcirc: pytket.circuit.Circuit = None)

Applies simplifications enabled by knowledge of qubit state and discarded qubits.

Param

allow_classical: allow replacement of measurements on known state with classical set-bit operations

Parameters

xcirc – 1-qubit circuit implementing an X gate in the transformed circuit (if omitted, an X gate is used)

Returns

a pass to perform the simplification

pytket.passes.DecomposeArbitrarilyControlledGates()

Decomposes CnX and CnRy gates into Ry, CX, H, T and Tdg gates.

pytket.passes.DecomposeBoxes()

Replaces all boxes by their decomposition into circuits.

pytket.passes.DecomposeClassicalExp()

Replaces each ClassicalExpBox by a sequence of classical gates.

pytket.passes.DecomposeMultiQubitsCX()

Converts all multi-qubit gates into CX and single-qubit gates.

pytket.passes.DecomposeSingleQubitsTK1()

Converts all single-qubit gates into TK1 gates.

pytket.passes.DecomposeSwapsToCXs(arc: pytket.routing.Architecture, respect_direction: bool = False)

Construct a pass to decompose SWAP and BRIDGE gates to CX gates, constraining connectivity to an Architecture, optionally taking the directedness of the connectivity graph into account.

Parameters
• arc – The architecture to use for connectivity information.

• respect_direction – Optionally takes the directedness of the connectivity graph into account.

Returns

a pass to perform the decomposition

pytket.passes.DecomposeSwapsToCircuit(replacement_circuit: pytket.circuit.Circuit)
Parameters

replacement_circuit – An equivalent circuit to replace a SWAP gate with in the desired basis.

Returns

a pass to replace all SWAP gates with the given circuit

pytket.passes.DefaultMappingPass(arc: pytket.routing.Architecture)

Construct a pass to relabel Circuit Qubits to Architecture Nodes, and then route to the connectivity graph of the given :py:class: ‘Architecture’. Edge direction is ignored. Placement used is GraphPlacement.

Parameters

arc – The Architecture used for connectivity information.

Returns

a pass to perform the remapping

pytket.passes.DelayMeasures()

Commutes Measure operations to the end of the circuit. Throws an exception when this is not possible because of gates following the measure which are dependent on either the resulting quantum state or classical values.

pytket.passes.EulerAngleReduction(q: pytket.circuit.OpType, p: pytket.circuit.OpType, strict: bool = False)

Uses Euler angle decompositions to squash all chains of P and Q rotations, where P,Q ∈ {Rx,Ry,Rz}. By default (strict=False), this pass will try to decompose the chains into pairs of -P-Q- or -Q-P- rotations, commuting any third rotation past multi-qubit gates. If strict=True, all chains will be decomposed to P-Q-P triples and no further optimisation is performed.

Parameters
• q – The type of the Q rotation (Q ∈ {Rx,Ry,Rz}).

• p – The type of the P rotation (P ∈ {Rx,Ry,Rz}, P ≠ Q).

• strict – Optionally performs strict P-Q-P Euler decomposition

Returns

a pass that squashes chains of P and Q rotations

pytket.passes.FlattenRegisters()

Merges all quantum and classical registers into their respective default registers with contiguous indexing.

pytket.passes.FullMappingPass(arc: pytket.routing.Architecture, placer: pytket.routing.Placement, **kwargs)

Construct a pass to relabel Circuit Qubits to Architecture Nodes, and then route to the connectivity graph of an Architecture. Edge direction is ignored.

Parameters
• arc – The architecture to use for connectivity information.

• placer – The Placement used for relabelling.

Returns

a pass to perform the remapping

pytket.passes.FullPeepholeOptimise()

Performs peephole optimisation including resynthesis of 2- and 3-qubit gate sequences, and converts to a circuit containing only CX and TK1 gates.

pytket.passes.GuidedPauliSimp(strat: pytket.transform.PauliSynthStrat = PauliSynthStrat.Sets, cx_config: pytket.transform.CXConfigType = CXConfigType.Snake)

Applies the PauliSimp optimisation pass to any region of the circuit contained within a CircBox. This can be useful to focus the synthesis to target specific sets of commuting operations, rather than the default greedy approach.

Parameters
• strat – A synthesis strategy for the Pauli graph.

• cx_config – A configuration of CXs to convert Pauli gadgets into.

Returns

a pass to perform the simplification

pytket.passes.KAKDecomposition(cx_fidelity: float = 1.0)

Construct an optimisation pass that performs a Cartan/KAK Decomposition for 2 qubit gate sequences, well explained in Robert Tucci (https://arxiv.org/abs/quant-ph/0507171). Given a circuit with CXs, SWAPs and any single-qubit gates, produces a circuit with the same gates. The optional parameter cx_fidelity must be between 0 and 1 and should be the estimated CX gate fidelity of the noisy quantum hardware to be compiled for. It is used to trade off decomposition accuracy for smaller CX gate count. If cx_fidelity < 1, the resulting decomposition will optimise the expected circuit fidelity. In this case, the output circuit will not be logically equivalent.This will not preserve CX orientation.

Parameters

cx_fidelity – The estimated gate fidelity

Returns

a KAK Decomposition pass using the given CX gate fidelity

Construct a pass that synthesises phase gadgets and converts to a circuit containing only CX and TK1 gates.

Parameters

cx_config – A configuration of CXs to convert phase gadgets into.

Returns

a pass to perform the synthesis

pytket.passes.PauliSimp(strat: pytket.transform.PauliSynthStrat = PauliSynthStrat.Sets, cx_config: pytket.transform.CXConfigType = CXConfigType.Snake)

Construct a pass that converts a circuit into a graph of Pauli gadgets to account for commutation and phase folding, and resynthesises them as either individual gagdets, pairwise constructions, or by diagonalising sets of commuting gadgets.

This pass will not preserve the global phase of the circuit.

Parameters
• strat – A synthesis strategy for the Pauli graph.

• cx_config – A configuration of CXs to convert Pauli gadgets into.

Returns

a pass to perform the simplification

pytket.passes.PauliSquash(strat: pytket.transform.PauliSynthStrat = PauliSynthStrat.Sets, cx_config: pytket.transform.CXConfigType = CXConfigType.Snake)

Applies PauliSimp() followed by FullPeepholeOptimise().

Parameters
• strat – a synthesis strategy for the Pauli graph

• cx_config – a configuration of CXs to convert Pauli gadgets into

Returns

a pass to perform the simplification

pytket.passes.PeepholeOptimise2Q()

Performs peephole optimisation including resynthesis of 2-qubit gate sequences, and converts to a circuit containing only CX and TK1 gates.

pytket.passes.PlacementPass(placer: pytket.routing.Placement)
Parameters

placer – The Placement used for relabelling.

Returns

a pass to relabel Circuit Qubits to Architecture Nodes

pytket.passes.RebaseCirq()

Converts all gates to CZ, PhasedX and Rz.

pytket.passes.RebaseCustom(multiqs: , cx_replacement: pytket.circuit.Circuit, singleqs: , tk1_replacement: Callable[[Expression, Expression, Expression], pytket.circuit.Circuit])

Construct a custom rebase pass. This pass: (1) decomposes multi-qubit gates not in the set of gate types multiqs to CX gates; (2) if CX is not in multiqs, replaces CX gates with cx_replacement; (3) converts any single-qubit gates not in the gate type set singleqs to the form $$\mathrm{Rz}(a)\mathrm{Rx}(b)\mathrm{Rz}(c)$$ (in matrix-multiplication order, i.e. reverse order in the circuit); (4) applies the tk1_replacement function to each of these triples $$(a,b,c)$$ to generate replacement circuits.

Parameters
• multiqs – The allowed multi-qubit operations in the rebased circuit.

• cx_replacement – The equivalent circuit to replace a CX gate in the desired basis.

• singleqs – The allowed single-qubit operations in the rebased circuit.

• tk1_replacement – A function which, given the parameters of an Rz(a)Rx(b)Rz(c) triple, returns an equivalent circuit in the desired basis.

Returns

a pass that rebases to the given gate set

pytket.passes.RebaseHQS()

Converts all gates to ZZMax, PhasedX and Rz.

pytket.passes.RebaseOQC()

Converts all gates to ECR, Rz and SX.

pytket.passes.RebaseProjectQ()

Converts all gates to SWAP, CRz, CX, CZ, H, X, Y, Z, S, T, V, Rx, Ry and Rz.

pytket.passes.RebasePyZX()

Converts all gates to SWAP, CX, CZ, H, X, Z, S, T, Rx and Rz.

pytket.passes.RebaseQuil()

Converts all gates to CZ, Rx and Rz.

pytket.passes.RebaseTket()

Converts all gates to CX and tk1.

pytket.passes.RebaseUFR()

Converts all gates to CX, Rz and H.

pytket.passes.RebaseUMD()

Converts all gates to XXPhase, PhasedX and Rz.

pytket.passes.RemoveBarriers()

A pass to remove all barrier instructions from the circuit.

A pass to remove all operations that have no OpType.Output or OpType.ClOutput in their causal future (in other words, all operations whose causal future is discarded).

pytket.passes.RemoveRedundancies()

Removes gate-inverse pairs, merges rotations, removes identity rotations, and removes redundant gates before measurement. Does not add any new gate types.

When merging rotations with the same op group name, the mergedoperation keeps the same name.

pytket.passes.RenameQubitsPass(qubit_map: )
Parameters

qubit_map – map from old to new qubit names

pytket.passes.RoutingPass(arc: pytket.routing.Architecture, **kwargs)

Construct a pass to route to the connectivity graph of an Architecture. Edge direction is ignored.

Parameters
• arc – The architecture to use for connectivity information.

Returns

a pass that routes to the given device architecture

pytket.passes.SimplifyInitial(allow_classical: bool = True, create_all_qubits: bool = False, remove_redundancies: bool = True, xcirc: pytket.circuit.Circuit = None)

Simplify the circuit using knowledge of qubit state.

Param

allow_classical: allow replacement of measurements on known state with classical set-bit operations

Param

create_all_qubits: automatically annotate all qubits as initialized to the zero state

Parameters
• remove_redundancies – apply a RemoveRedundancies() pass after the initial simplification

• xcirc – 1-qubit circuit implementing an X gate in the transformed circuit (if omitted, an X gate is used)

Returns

a pass to perform the simplification

pytket.passes.SimplifyMeasured()

A pass to replace all ‘classical maps’ followed by measure operations whose quantum output is discarded with classical operations following the measure. (A ‘classical map’ is a quantum operation that acts as a permutation of the computational basis states followed by a diagonal operation.)

pytket.passes.SquashCustom(singleqs: , tk1_replacement: Callable[[Expression, Expression, Expression], pytket.circuit.Circuit])

Squash sequences of single qubit gates from the target gate set into an optimal form given by tk1_replacement.

Parameters
• singleqs – The types of single qubit gates in the target gate set. This pass will only affect sequences of gates that are already in this set.

• tk1_replacement – A function which, given the parameters of an Rz(a)Rx(b)Rz(c) triple, returns an equivalent circuit in the desired basis.

pytket.passes.SquashHQS()

Squash Rz and PhasedX gate sequences into an optimal form.

pytket.passes.SquashTK1()

Squash sequences of single-qubit gates to TK1 gates.

pytket.passes.SynthesiseHQS()

Optimises and converts a circuit consisting of CX and single-qubit gates into one containing only ZZMax, PhasedX and Rz.

pytket.passes.SynthesiseOQC()

Optimises and converts all gates to ECR, Rz and SX.

pytket.passes.SynthesiseTket()

Optimises and converts all gates to CX and TK1 gates.

pytket.passes.SynthesiseUMD()

Optimises and converts all gates to XXPhase, PhasedX and Rz.

pytket.passes.ThreeQubitSquash(allow_swaps: bool = True)

Squash three-qubit subcircuits into subcircuits having fewer CX gates, when possible, and apply Clifford simplification.

Param

allow_swaps whether to allow implicit wire swaps