pytket.passes

In pytket, compilation passes perform in-place transformations of circuits. From a user’s point of view, passes are similar to transforms; however passes allow for additional predicate checking and compositionality.

There are passes such as FullPeepholeOptimise and KAKDecomposition which are designed for general purpose circuit optimisation.

Also there are special purpose passes such as OptimisePhaseGadgets and PauliSimp which perform optimisation by targeting phase gadget and Pauli gadget structures within circuits. For more on these optimisation techniques see the corresponding publication.

Rebase passes can be used to convert a circuit to a desired gateset. See RebaseCustom and auto_rebase_pass.

For more on pytket passes see the compilation section of the user manual or the notebook tutorials

class pytket._tket.passes.BasePass

Base class for passes.

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

Overloaded function.

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

Apply to a CompilationUnit.

Returns:

True if the pass modified the circuit. Note that in some cases the method may return True even when the circuit is unmodified (but a return value of False definitely implies no modification).

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

Apply to a Circuit in-place.

Returns:

True if pass modified the circuit, else False

  1. apply(self: pytket._tket.passes.BasePass, circuit: pytket._tket.circuit.Circuit, before_apply: Callable[[pytket._tket.predicates.CompilationUnit, object], None], after_apply: Callable[[pytket._tket.predicates.CompilationUnit, object], 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(base_pass_dict: dict, custom_deserialisation: dict[str, Callable[[pytket._tket.circuit.Circuit], pytket._tket.circuit.Circuit]] = {}) pytket._tket.passes.BasePass

Construct a new Pass instance from a JSON serializable dictionary representation. custom_deserialisation is a map between CustomPass label attributes and a Circuit to Circuit function matching the CustomPass transform argument. This allows the construction of some CustomPass from JSON. CustomPass without a matching entry in custom_deserialisation will be rejected.

to_dict(self: pytket._tket.passes.BasePass) object
Returns:

A JSON serializable dictionary representation of the Pass.

class pytket._tket.passes.CNotSynthType

Members:

SWAP : swap-based algorithm for CNOT synthesis

HamPath : Hamilton-path-based method for CNOT synthesis; this method will fail if there is no Hamilton path in the given architecture

Rec : recursive Steiner–Gauss method for CNOT synthesis

__init__(self: pytket._tket.passes.CNotSynthType, value: int) None
property name
class pytket._tket.passes.RepeatPass

Repeat a pass until its apply() method returns False, or if strict_check is True until it stops modifying the circuit.

__init__(self: pytket._tket.passes.RepeatPass, compilation_pass: pytket._tket.passes.BasePass, strict_check: bool = False) None

Construct from a compilation pass.

get_pass(self: pytket._tket.passes.RepeatPass) pytket._tket.passes.BasePass
Returns:

The underlying compilation pass.

class pytket._tket.passes.RepeatUntilSatisfiedPass

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

__init__(*args, **kwargs)

Overloaded function.

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

Construct from a compilation pass and a predicate.

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

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

get_pass(self: pytket._tket.passes.RepeatUntilSatisfiedPass) pytket._tket.passes.BasePass
Returns:

The underlying compilation pass.

get_predicate(self: pytket._tket.passes.RepeatUntilSatisfiedPass) pytket._tket.predicates.Predicate
Returns:

The underlying predicate.

class pytket._tket.passes.RepeatWithMetricPass

Repeat a compilation pass until the given metric stops decreasing.

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

Construct from a compilation pass and a metric function.

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

The underlying metric.

get_pass(self: pytket._tket.passes.RepeatWithMetricPass) pytket._tket.passes.BasePass
Returns:

The underlying compilation pass.

class pytket._tket.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._tket.passes.SafetyMode, value: int) None
property name
class pytket._tket.passes.SequencePass

A sequence of compilation passes.

__init__(self: pytket._tket.passes.SequencePass, pass_list: Sequence[pytket._tket.passes.BasePass], strict: bool = True) None

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

Parameters:
  • pass_list – sequence of passes

  • strict – if True (the default), check that all postconditions and preconditions of the passes in the sequence are compatible and raise an exception if not.

Returns:

a pass that applies the sequence

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

The underlying sequence of passes.

to_dict(self: pytket._tket.passes.SequencePass) object
Returns:

A JSON serializable dictionary representation of the SequencePass.

pytket._tket.passes.AASRouting(arc: pytket._tket.architecture.Architecture, **kwargs) pytket._tket.passes.BasePass

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:

  • (unsigned) lookahead=1: parameter for the recursive iteration

  • (CNotSynthType) cnotsynthtype=CNotSynthType.Rec: CNOT synthesis type

NB: The circuit needs to have at most as many qubits as the architecture has nodes. The resulting circuit will always have the same number of qubits as the architecture has nodes, even if the input circuit had fewer.

Parameters:
  • arc – target architecture

  • **kwargs – parameters for routing (described above)

Returns:

a pass to perform the remapping

pytket._tket.passes.AutoRebase(gateset: set[pytket._tket.circuit.OpType], allow_swaps: bool = False) pytket._tket.passes.BasePass

Attempt to generate a rebase pass automatically for the given target gateset. Checks if there are known existing decompositions to target gateset and TK1 to target gateset and uses those to construct a custom rebase. Raises an error if no known decompositions can be found, in which case try using RebaseCustom with your own decompositions.

Parameters:
  • gateset – Set of supported OpTypes, target gate set. (in addition, Measure and Reset operations are always allowed and are left alone; conditional operations may be present; and Phase gates may also be introduced by the rebase)

  • allow_swaps – Whether to allow implicit wire swaps. Default to False.

pytket._tket.passes.AutoSquash(singleqs: set[pytket._tket.circuit.OpType]) pytket._tket.passes.BasePass

Attempt to generate a squash pass automatically for the given target single qubit gateset. Raises an error if no known TK1 decomposition can be found based on the given gateset, in which case try using SquashCustom with your own decomposition.

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.

pytket._tket.passes.CXMappingPass(arc: pytket._tket.architecture.Architecture, placer: pytket._tket.placement.Placement, **kwargs) pytket._tket.passes.BasePass

Construct a pass to convert all gates to CX, relabel Circuit Qubits to 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: (bool)directed_cx=false, (bool)delay_measures=true

Returns:

a pass to perform the remapping

pytket._tket.passes.CliffordPushThroughMeasures() pytket._tket.passes.BasePass

An optimisation pass that resynthesise a Clifford subcircuit before end of circuit Measurement operations by implementing the action of the Clifford as a mutual diagonalisation circuit and a permutation on output measurements realised as a series of classical operations. : return: a pass to simplify end of circuit Clifford gates.

pytket._tket.passes.CliffordResynthesis(transform: Callable[[pytket._tket.circuit.Circuit], pytket._tket.circuit.Circuit] | None = None, allow_swaps: bool = True) pytket._tket.passes.BasePass

An optimisation pass that resynthesises Clifford subcircuits, trying to reduce the 2-qubit gate count as much as possible.

Parameters:
  • transform – optional user-provided resynthesis method to apply to all Clifford subcircuits (a function taking a Clifford circuit as an argument and returning an equivalent circuit); if not provided, a default resynthesis method is applied

  • allow_swaps – whether the rewriting may introduce wire swaps (only relevant to the default resynthesis method used when the transform argument is not provided)

Returns:

a pass to perform the rewriting

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

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._tket.passes.CnXPairwiseDecomposition() pytket._tket.passes.BasePass

Decompose CnX gates to 2-qubit gates `fand single qubit gates. For every two CnX gates, reorder their control qubits to improve the chance of gate cancellation

pytket._tket.passes.CommuteThroughMultis() pytket._tket.passes.BasePass

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

pytket._tket.passes.ComposePhasePolyBoxes(min_size: int = 0) pytket._tket.passes.BasePass

Pass to convert a given Circuit to the CX, Rz, H gateset and compose phase polynomial boxes from the groups of the CX+Rz gates.

  • (unsigned) min_size=0: minimal number of CX gates in each phase polynominal box: groups with a smaller number of CX gates are not affected by this transformation

Parameters:

**kwargs – parameters for composition (described above)

Returns:

a pass to perform the composition

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

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

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

  • 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._tket.passes.CustomPass(transform: Callable[[pytket._tket.circuit.Circuit], pytket._tket.circuit.Circuit], label: str = '') pytket._tket.passes.BasePass

Generate a custom pass from a user-provided circuit transfomation function.

It is the caller’s responsibility to provide a valid transform.

Parameters:
  • transform – function taking a Circuit as an argument and returning a new transformed circuit

  • label – optional label for the pass

Returns:

a pass to perform the transformation

pytket._tket.passes.CustomRoutingPass(arc: pytket._tket.architecture.Architecture, config: Sequence[pytket._tket.mapping.RoutingMethod]) pytket._tket.passes.BasePass

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

Returns:

a pass that routes to the given device architecture

pytket._tket.passes.DecomposeArbitrarilyControlledGates() pytket._tket.passes.BasePass

Decomposes CCX, CnX, CnY, CnZ, CnRy, CnRz and CnRx gates into CX and single-qubit gates.

pytket._tket.passes.DecomposeBoxes(excluded_types: set[pytket._tket.circuit.OpType] = set(), excluded_opgroups: set[str] = set()) pytket._tket.passes.BasePass

Recursively replaces all boxes by their decomposition into circuits.

Parameters:
  • excluded_types – box `OpType`s excluded from decomposition

  • excluded_opgroups – opgroups excluded from decomposition

pytket._tket.passes.DecomposeClassicalExp() pytket._tket.passes.BasePass

Replaces each ClassicalExpBox and ClExprOp by a sequence of classical gates.

pytket._tket.passes.DecomposeMultiQubitsCX() pytket._tket.passes.BasePass

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

pytket._tket.passes.DecomposeSingleQubitsTK1() pytket._tket.passes.BasePass

Converts all single-qubit gates into TK1 gates.

pytket._tket.passes.DecomposeSwapsToCXs(arc: pytket._tket.architecture.Architecture, respect_direction: bool = False) pytket._tket.passes.BasePass

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._tket.passes.DecomposeSwapsToCircuit(replacement_circuit: pytket._tket.circuit.Circuit) pytket._tket.passes.BasePass
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._tket.passes.DecomposeTK2(allow_swaps: bool = True, **kwargs) pytket._tket.passes.BasePass

Decompose each TK2 gate into two-qubit gates.

Gate fidelities can be passed as keyword arguments to perform noise-aware decompositions. If the fidelities of several gate types are provided, the best will be chosen.

We currently support CX_fidelity, ZZMax_fidelity and ZZPhase_fidelity. If provided, the CX and ZZMax fidelities must be given by a single floating point fidelity. The ZZPhase fidelity is given as a lambda float -> float, mapping a ZZPhase angle parameter to its fidelity, or by a single float. These parameters will be used to return the optimal decomposition of each TK2 gate, taking noise into consideration.

If no fidelities are provided, the TK2 gates will be decomposed exactly using CX gates. For equal fidelities, ZZPhase will be prefered over ZZMax and CX if the decomposition results in fewer two-qubit gates.

All TK2 gate parameters must be normalised, i.e. they must satisfy NormalisedTK2Predicate. (This can be achieved by applying the NormaliseTK2() pass beforehand.)

Using the allow_swaps=True (default) option, qubits will be swapped when convenient to reduce the two-qubit gate count of the decomposed TK2.

If the TK2 angles are symbolic values, the decomposition will be exact (i.e. not noise-aware). It is not possible in general to obtain optimal decompositions for arbitrary symbolic parameters, so consider substituting for concrete values if possible.

Parameters:

allow_swaps – Whether to allow implicit wire swaps.

pytket._tket.passes.DefaultMappingPass(arc: pytket._tket.architecture.Architecture, delay_measures: bool = True) pytket._tket.passes.BasePass

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

Parameters:
  • arc – The Architecture used for connectivity information.

  • delay_measures – Whether to commute measurements to the end of the circuit, defaulting to true.

Returns:

a pass to perform the remapping

pytket._tket.passes.DelayMeasures(allow_partial: bool = True) pytket._tket.passes.BasePass

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.

Parameters:

allow_partial – Whether to allow measurements that cannot be commuted to the end, and delay them as much as possible instead. If false, the pass includes a CommutableMeasuresPredicate precondition.

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

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._tket.passes.FlattenRegisters() pytket._tket.passes.BasePass

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

pytket._tket.passes.FlattenRelabelRegistersPass(label: str = 'q', relabel_classical_expressions: bool = True) pytket._tket.passes.BasePass

Removes empty Quantum wires from the Circuit and relabels all Qubit to a register from passed name.

Parameters:
  • label – Name to relabel remaining Qubit to, default ‘q’.

  • relabel_classical_expressions – Whether to relabel arguments of expressions held in ClassicalExpBox.

Returns:

A pass that removes empty wires and relabels.

pytket._tket.passes.FullMappingPass(arc: pytket._tket.architecture.Architecture, placer: pytket._tket.placement.Placement, config: Sequence[pytket._tket.mapping.RoutingMethod]) pytket._tket.passes.BasePass

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.

  • config – Parameters for routing, a list of RoutingMethod, each method is checked and run if applicable in turn.

Returns:

a pass to perform the remapping

pytket._tket.passes.FullPeepholeOptimise(allow_swaps: bool = True, target_2qb_gate: pytket._tket.circuit.OpType = <OpType.CX: 45>) pytket._tket.passes.BasePass

Performs peephole optimisation including resynthesis of 2- and 3-qubit gate sequences, and converts to a circuit containing only the given 2-qubit gate (which may be CX or TK2) and TK1 gates.

Parameters:

allow_swaps – whether to allow implicit wire swaps

pytket._tket.passes.GlobalisePhasedX(squash: bool = True) pytket._tket.passes.BasePass

Turns all PhasedX and NPhasedX gates into global gates

Replaces any PhasedX gates with global NPhasedX gates. By default, this transform will squash all single-qubit gates to PhasedX and Rz gates before proceeding further. Existing non-global NPhasedX will not be preserved. This is the recommended setting for best performance. If squashing is disabled, each non-global PhasedX gate will be replaced with two global NPhasedX, but any other gates will be left untouched.

DEPRECATED: This pass will be removed no earlier than three months after the pytket 1.35 release.

Parameters:

squash – Whether to squash the circuit in pre-processing (default: true).

If squash=true (default), the GlobalisePhasedX transform’s apply method will always return true. For squash=false, apply() will return true if the circuit was changed and false otherwise.

It is not recommended to use this pass with symbolic expressions, as in certain cases a blow-up in symbolic expression sizes may occur.

pytket._tket.passes.GreedyPauliSimp(discount_rate: float = 0.7, depth_weight: float = 0.3, max_lookahead: int = 500, max_tqe_candidates: int = 500, seed: int = 0, allow_zzphase: bool = False, thread_timeout: int = 100, only_reduce: bool = False, trials: int = 1) pytket._tket.passes.BasePass

Construct a pass that converts a circuit into a graph of Pauli gadgets to account for commutation and phase folding, and resynthesises them using a greedy algorithm adapted from arxiv.org/abs/2103.08602. The method for synthesising the final Clifford operator is adapted from arxiv.org/abs/2305.10966.

Parameters:
  • discount_rate – Rate used to discount the cost impact from gadgets that are further away. Default to 0.7.

  • depth_weight – Degree of depth optimisation. Default to 0.3.

  • max_tqe_candidates – Maximum number of 2-qubit Clifford gate candidates to evaluate at each step. Default to 500.

  • max_lookahead – Maximum lookahead when evaluating each Clifford gate candidate. Default to 500.

  • seed – Unsigned integer seed used for sampling candidates and tie breaking. Default to 0.

  • allow_zzphase – If set to True, allows the algorithm to implement 2-qubit rotations using ZZPhase gates when deemed optimal. Defaults to False.

  • thread_timeout – Sets maximum out of time spent finding a single solution in one thread.

  • only_reduce – Only returns modified circuit if it has fewer two-qubit gates.

  • trials – Sets maximum number of found solutions. The smallest circuit is returned, prioritising the number of 2qb-gates, then the number of gates, then the depth.

Returns:

a pass to perform the simplification

pytket._tket.passes.GuidedPauliSimp(strat: pytket._tket.transform.PauliSynthStrat = <PauliSynthStrat.Sets: 2>, cx_config: pytket._tket.circuit.CXConfigType = <CXConfigType.Snake: 0>) pytket._tket.passes.BasePass

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._tket.passes.KAKDecomposition(*args, **kwargs)

Overloaded function.

  1. KAKDecomposition(target_2qb_gate: pytket._tket.circuit.OpType = <OpType.CX: 45>, cx_fidelity: float = 1.0, allow_swaps: bool = True) -> pytket._tket.passes.BasePass

Squash sequences of two-qubit operations into minimal form.

Pass to squash together sequences of single- and two-qubit gates into minimal form. Can decompose to TK2 or CX gates.

Two-qubit operations can always be expressed in a minimal form of maximum three CXs, or as a single TK2 gate (a result also known as the KAK or Cartan decomposition).

It is in general recommended to squash to TK2 gates, and to then use the DecomposeTK2 pass for noise-aware decompositions to other gatesets. For backward compatibility, decompositions to CX are also supported. In this case, cx_fidelity can be provided to perform approximate decompositions to CX gates.

When decomposing to TK2 gates, any sequence of two or more two-qubit gates on the same set of qubits are replaced by a single TK2 gate. When decomposing to CX, the substitution is only performed if it results in a reduction of the number of CX gates, or if at least one of the two-qubit gates is not a CX.

Using the allow_swaps=True (default) option, qubits will be swapped when convenient to further reduce the two-qubit gate count (only applicable when decomposing to CX gates).

Note that gates containing symbolic parameters are not squashed.

Parameters:
  • target_2qb_gate – OpType to decompose to. Either TK2 or CX.

  • cx_fidelity – Estimated CX gate fidelity, used when target_2qb_gate=CX.

  • allow_swaps – Whether to allow implicit wire swaps.

  1. KAKDecomposition(cx_fidelity: float) -> pytket._tket.passes.BasePass

pytket._tket.passes.NaivePlacementPass(architecture: pytket._tket.architecture.Architecture) pytket._tket.passes.BasePass
Parameters:

architecture – The Architecture used for relabelling.

Returns:

a pass to relabel Circuit Qubits to Architecture Nodes

pytket._tket.passes.NormaliseTK2() pytket._tket.passes.BasePass

Normalises all TK2 gates.

TK2 gates have three angles in the interval [0, 4], but these can always be normalised to be within the so-called Weyl chamber by adding single-qubit gates.

More precisely, the three angles a, b, c of TK2(a, b, c) are normalised exactly when the two following conditions are met:
  • numerical values must be in the Weyl chamber, ie 1/2 >= a >= b >= |c|,

  • symbolic values must come before any numerical value in the array.

After this pass, all TK2 angles will be normalised and the circuit will satisfy NormalisedTK2Predicate.

pytket._tket.passes.OptimisePhaseGadgets(cx_config: pytket._tket.circuit.CXConfigType = <CXConfigType.Snake: 0>) pytket._tket.passes.BasePass

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

Parameters:

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

Returns:

a pass to perform the synthesis

pytket._tket.passes.PauliExponentials(strat: pytket._tket.transform.PauliSynthStrat = <PauliSynthStrat.Sets: 2>, cx_config: pytket._tket.circuit.CXConfigType = <CXConfigType.Snake: 0>) pytket._tket.passes.BasePass

Construct a pass that converts a circuit into a graph of Pauli exponential boxes, with information

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._tket.passes.PauliSimp(strat: pytket._tket.transform.PauliSynthStrat = <PauliSynthStrat.Sets: 2>, cx_config: pytket._tket.circuit.CXConfigType = <CXConfigType.Snake: 0>) pytket._tket.passes.BasePass

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._tket.passes.PauliSquash(strat: pytket._tket.transform.PauliSynthStrat = <PauliSynthStrat.Sets: 2>, cx_config: pytket._tket.circuit.CXConfigType = <CXConfigType.Snake: 0>) pytket._tket.passes.BasePass

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._tket.passes.PeepholeOptimise2Q(allow_swaps: bool = True) pytket._tket.passes.BasePass

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

Parameters:

allow_swaps – whether to allow implicit wire swaps

pytket._tket.passes.PlacementPass(placer: pytket._tket.placement.Placement) pytket._tket.passes.BasePass
Parameters:

placer – The Placement used for relabelling.

Returns:

a pass to relabel Circuit Qubits to Architecture Nodes

pytket._tket.passes.RebaseCustom(*args, **kwargs)

Overloaded function.

  1. RebaseCustom(gateset: set[pytket._tket.circuit.OpType], cx_replacement: pytket._tket.circuit.Circuit, tk1_replacement: Callable[[typing.Union[sympy.Expr, float], typing.Union[sympy.Expr, float], typing.Union[sympy.Expr, float]], pytket._tket.circuit.Circuit]) -> pytket._tket.passes.BasePass

Construct a custom rebase pass, given user-defined rebases for TK1 and CX. This pass:

  1. decomposes multi-qubit gates not in the set of gate types gateset to CX gates;

  2. if CX is not in gateset, replaces CX gates with cx_replacement;

  3. converts any single-qubit gates not in the gate type set 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:
  • gateset – the allowed operations in the rebased circuit (in addition, Measure and Reset operations are always allowed and are left alone; conditional operations may be present; and Phase gates may also be introduced by the rebase)

  • cx_replacement – the equivalent circuit to replace a CX gate using two qubit gates from the desired basis (can use any single qubit OpTypes)

  • 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 (possibly including conditional and phase operations, and Measure and Reset

  1. RebaseCustom(gateset: set[pytket._tket.circuit.OpType], tk2_replacement: Callable[[typing.Union[sympy.Expr, float], typing.Union[sympy.Expr, float], typing.Union[sympy.Expr, float]], pytket._tket.circuit.Circuit], tk1_replacement: Callable[[typing.Union[sympy.Expr, float], typing.Union[sympy.Expr, float], typing.Union[sympy.Expr, float]], pytket._tket.circuit.Circuit]) -> pytket._tket.passes.BasePass

Construct a custom rebase pass, given user-defined rebases for TK1 and TK2. This pass:

  1. decomposes multi-qubit gates not in the set of gate types gateset to TK2 gates;

  2. if TK2 is not in gateset, replaces TK2(a,b,c) gates via the tk2_replacement function;

  3. converts any single-qubit gates not in the gate type set to TK1;

  4. if TK2 is not in gateset. applies the tk1_replacement function to each TK1(a,b,c).

Parameters:
  • gateset – the allowed operations in the rebased circuit (in addition, Measure and Reset always allowed and are left alone; conditional operations may be present; and Phase gates may also be introduced by the rebase)

  • tk2_replacement – a function which, given the parameters (a,b,c) of an XXPhase(a)YYPhase(b)ZZPhase(c) triple, returns an equivalent circuit in the desired basis

  • tk1_replacement – a function which, given the parameters (a,b,c) 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 (possibly including conditional and phase operations, and Measure and Reset)

pytket._tket.passes.RebaseTket() pytket._tket.passes.BasePass

Converts all gates to CX, TK1 and Phase. (Any Measure and Reset operations are left untouched; Conditional gates are also allowed.)

pytket._tket.passes.RemoveBarriers() pytket._tket.passes.BasePass

A pass to remove all barrier instructions from the circuit.

pytket._tket.passes.RemoveDiscarded() pytket._tket.passes.BasePass

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._tket.passes.RemoveImplicitQubitPermutation() pytket._tket.passes.BasePass

Remove any implicit qubit permutation by appending SWAP gates.

Note that if the circuit contains measurements, they may become mid-circuit measurements in the transformed circuit.

pytket._tket.passes.RemoveRedundancies() pytket._tket.passes.BasePass

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 merged operation keeps the same name.

pytket._tket.passes.RenameQubitsPass(qubit_map: dict[pytket._tket.unit_id.Qubit, pytket._tket.unit_id.Qubit]) pytket._tket.passes.BasePass

Rename some or all qubits.

Parameters:

qubit_map – map from old to new qubit names

pytket._tket.passes.RoundAngles(n: int, only_zeros: bool = False) pytket._tket.passes.BasePass

Round angles to the nearest \(\pi / 2^n\).

Parameters:
  • n – precision parameter, must be >= 0 and < 32

  • only_zeros – if True, only round angles less than \(\pi / 2^{n+1}\) to zero, leave other angles alone (default False)

pytket._tket.passes.RoutingPass(arc: pytket._tket.architecture.Architecture) pytket._tket.passes.BasePass

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

Returns:

a pass that routes to the given device architecture

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

Simplify the circuit using knowledge of qubit state.

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

  • create_all_qubits – automatically annotate all qubits as initialized to the zero state

  • 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._tket.passes.SimplifyMeasured() pytket._tket.passes.BasePass

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._tket.passes.SquashCustom(singleqs: set[pytket._tket.circuit.OpType], tk1_replacement: Callable[[Union[sympy.Expr, float], Union[sympy.Expr, float], Union[sympy.Expr, float]], pytket._tket.circuit.Circuit], always_squash_symbols: bool = False) pytket._tket.passes.BasePass

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.

  • always_squash_symbols – If true, always squash symbolic gates regardless of the blow-up in complexity. Default is false, meaning that symbolic gates are only squashed if doing so reduces the overall symbolic complexity.

pytket._tket.passes.SquashRzPhasedX() pytket._tket.passes.BasePass

Squash single qubit gates into PhasedX and Rz gates. Also remove identity gates. Commute Rz gates to the back if possible.

pytket._tket.passes.SquashTK1() pytket._tket.passes.BasePass

Squash sequences of single-qubit gates to TK1 gates.

pytket._tket.passes.SynthesiseTK() pytket._tket.passes.BasePass

Optimises and converts all gates to TK2, TK1 and Phase gates.

pytket._tket.passes.SynthesiseTket() pytket._tket.passes.BasePass

Optimises and converts all gates to CX, TK1 and Phase gates.

pytket._tket.passes.SynthesiseUMD() pytket._tket.passes.BasePass

Optimises and converts all gates to XXPhase, PhasedX, Rz and Phase. DEPRECATED: will be removed after pytket 1.32.

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

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

The circuit to which this is applied must consist of single-qubit, pure-classical and CX gates, and Measure, Collapse, Reset, Phase and conditional gates.

Parameters:

allow_swaps – whether to allow implicit wire swaps

pytket._tket.passes.ZXGraphlikeOptimisation() pytket._tket.passes.BasePass

Attempt to optimise the circuit by simplifying in ZX calculus and extracting a circuit back out. Due to limitations in extraction, may not work if the circuit contains created or discarded qubits. As a resynthesis pass, this will ignore almost all optimisations achieved beforehand and may increase the cost of the circuit.

pytket._tket.passes.ZZPhaseToRz() pytket._tket.passes.BasePass

Converts all ZZPhase gates in a circuit with angle 1 or -1 (half-turns) into two Rz gates each with a parameter value of 1 (half-turns). ZZPhase gates with parameter values other than 1 or -1 (half-turns) are left unchanged.

Returns:

a pass to convert ZZPhase gates to Rz.

class pytket.passes.PassSelector(passlist, score_func)[source]

Collection of pytket compilation passes which are all applied to the same circuit. The result of the compilation is the best circuit as selected by a given metric.

__init__(passlist, score_func)[source]

Constructs a PassSelector

Parameters:
  • passlist (list[BasePass]) – list of pytket compilation passes

  • score_func (Callable[[Circuit], int]) – function to score the results of the compilation (lower scores are preferred)

apply(circ)[source]

Compiles the given circuit with the best of the given passes.

Parameters:

circ (Circuit) – Circuit that should be compiled

Return type:

Circuit

Returns:

compiled circuit

get_scores()[source]
Return type:

list[int | None]

Returns:

scores of the circuit after compiling for each of the compilations passes

pytket.passes.script

pytket.passes.script.compilation_pass_from_script(script)[source]

Generate a compilation pass from a specification.

The specification must conform to a simple grammar. For example, the following are valid specifications:

  • “RemoveRedundancies”

  • “[RemoveBarriers, RemoveRedundancies]” (a sequence of passes)

  • “repeat(FullPeepholeOptimise)” (repeat a pass until it doesn’t change the circuit)

Sequences and repeats can be nested arbitrarily. Whitespace is ignored.

Most passes are specified using their Python names. For those that take enums as parameters, non-default values can be specified using their Python names:

  • “PauliSimp” (default parameters)

  • “PauliSimp(Pairwise, Tree)”

  • “EulerAngleReduction(Ry, Rz)”

For some passes with optional boolean parameters the name can be modified as follows:

  • “CliffordSimp” (default parameters)

  • “CliffordSimpNoSwaps”

  • “SimplifyInitial” (default parameters)

  • “SimplifyInitialNoClassical”

There is currently no support for passes requiring more complex parameters such as lambdas or circuits.

The full formal grammar can be inspected using compilation_pass_grammar().

Parameters:

script (str) – specification of pass

Return type:

BasePass

pytket.passes.script.compilation_pass_grammar()[source]

Formal grammar for specifying compilation passes.

This is the grammar assumed by complilation_pass_from_script().

Return type:

str

Returns:

grammar in extended Backus–Naur form

pytket.passes.auto_rebase

pytket.passes.auto_rebase.auto_rebase_pass(gateset, allow_swaps=False)[source]

Attempt to generate a rebase pass automatically for the given target gateset.

Checks if there are known existing decompositions to target gateset and TK1 to target gateset and uses those to construct a custom rebase. Raises an error if no known decompositions can be found, in which case try using RebaseCustom with your own decompositions.

In addition to the gate types in gateset, any Measure and Reset operations in the original circuit are retained. Conditional operations are also allowed. Phase gates may also be introduced.

Parameters:

gateset (FrozenSet[OpType]) – Set of supported OpTypes, target gate set.

Raises:

NoAutoRebase – No suitable decomposition found.

Returns:

Rebase pass.

Return type:

AutoRebase

Deprecated since version 1.29.0: Will be removed after pytket 1.33, please use AutoRebase instead.

pytket.passes.auto_rebase.auto_squash_pass(gateset)[source]

Attempt to generate a squash pass automatically for the given target single qubit gateset.

Parameters:

gateset (Set[OpType]) – Available single qubit gateset

Returns:

Squash to target gateset

Return type:

AutoSquash

Deprecated since version 1.29.0: Will be removed after pytket 1.33, please use AutoSquash instead.