pytket.passes

Contents

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.passes.BasePass#

Base class for passes.

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

Overloaded function.

  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 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.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, object], None], after_apply: Callable[[pytket.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(arg0: dict) pytket.passes.BasePass#

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

to_dict(self: pytket.passes.BasePass) dict#
Returns:

A JSON serializable dictionary representation of the Pass.

class pytket.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.passes.CNotSynthType, value: int) None#
property name#
class pytket.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.passes.RepeatPass, compilation_pass: pytket.passes.BasePass, strict_check: bool = False) None#

Construct from a compilation pass.

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

The underlying compilation pass.

class pytket.passes.RepeatUntilSatisfiedPass#

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

__init__(*args, **kwargs)#

Overloaded function.

  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) pytket.passes.BasePass#
Returns:

The underlying compilation pass.

get_predicate(self: pytket.passes.RepeatUntilSatisfiedPass) pytket.predicates.Predicate#
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) pytket.passes.BasePass#
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: Sequence[pytket.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.passes.SequencePass) list[pytket.passes.BasePass]#
Returns:

The underlying sequence of passes.

pytket.passes.AASRouting(arc: pytket.architecture.Architecture, **kwargs) pytket.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.passes.CXMappingPass(arc: pytket.architecture.Architecture, placer: pytket.placement.Placement, **kwargs) pytket.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.passes.CliffordPushThroughMeasures() pytket.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.passes.CliffordResynthesis(transform: Callable[[pytket.circuit.Circuit], pytket.circuit.Circuit] | None = None, allow_swaps: bool = True) pytket.passes.BasePass#

An optimisation pass that resynhesises all Clifford subcircuits and then applies some rewrite rules to simplify them further.

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.passes.CliffordSimp(allow_swaps: bool = True) pytket.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.passes.CnXPairwiseDecomposition() pytket.passes.BasePass#

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

pytket.passes.CommuteThroughMultis() pytket.passes.BasePass#

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

pytket.passes.ComposePhasePolyBoxes(min_size: int = 0) pytket.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.passes.ContextSimp(allow_classical: bool = True, xcirc: pytket.circuit.Circuit | None = None) pytket.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.passes.CustomPass(transform: Callable[[pytket.circuit.Circuit], pytket.circuit.Circuit], label: str = '') pytket.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.passes.CustomRoutingPass(arc: pytket.architecture.Architecture, config: Sequence[pytket.mapping.RoutingMethod]) pytket.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.passes.DecomposeArbitrarilyControlledGates() pytket.passes.BasePass#

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

pytket.passes.DecomposeBoxes(excluded_types: set[pytket.circuit.OpType] = set(), excluded_opgroups: set[str] = set()) pytket.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.passes.DecomposeClassicalExp() pytket.passes.BasePass#

Replaces each ClassicalExpBox by a sequence of classical gates.

pytket.passes.DecomposeMultiQubitsCX() pytket.passes.BasePass#

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

pytket.passes.DecomposeSingleQubitsTK1() pytket.passes.BasePass#

Converts all single-qubit gates into TK1 gates.

pytket.passes.DecomposeSwapsToCXs(arc: pytket.architecture.Architecture, respect_direction: bool = False) pytket.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.passes.DecomposeSwapsToCircuit(replacement_circuit: pytket.circuit.Circuit) pytket.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.passes.DecomposeTK2(allow_swaps: bool = True, **kwargs) pytket.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. 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.passes.DefaultMappingPass(arc: pytket.architecture.Architecture, delay_measures: bool = True) pytket.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.passes.DelayMeasures(allow_partial: bool = True) pytket.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.passes.EulerAngleReduction(q: pytket.circuit.OpType, p: pytket.circuit.OpType, strict: bool = False) pytket.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.passes.FlattenRegisters() pytket.passes.BasePass#

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

pytket.passes.FlattenRelabelRegistersPass(label: str = 'q') pytket.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’.

Returns:

A pass that removes empty wires and relabels.

pytket.passes.FullMappingPass(arc: pytket.architecture.Architecture, placer: pytket.placement.Placement, config: Sequence[pytket.mapping.RoutingMethod]) pytket.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.passes.FullPeepholeOptimise(allow_swaps: bool = True, target_2qb_gate: pytket.circuit.OpType = OpType.CX) pytket.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.passes.GlobalisePhasedX(squash: bool = True) pytket.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.

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.passes.GuidedPauliSimp(strat: pytket.transform.PauliSynthStrat = PauliSynthStrat.Sets, cx_config: pytket.circuit.CXConfigType = CXConfigType.Snake) pytket.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.passes.KAKDecomposition(*args, **kwargs)#

Overloaded function.

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

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

architecture – The Architecture used for relabelling.

Returns:

a pass to relabel Circuit Qubits to Architecture Nodes

pytket.passes.NormaliseTK2() pytket.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.passes.OptimisePhaseGadgets(cx_config: pytket.circuit.CXConfigType = CXConfigType.Snake) pytket.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.passes.PauliExponentials(strat: pytket.transform.PauliSynthStrat = PauliSynthStrat.Sets, cx_config: pytket.circuit.CXConfigType = CXConfigType.Snake) pytket.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.passes.PauliSimp(strat: pytket.transform.PauliSynthStrat = PauliSynthStrat.Sets, cx_config: pytket.circuit.CXConfigType = CXConfigType.Snake) pytket.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.passes.PauliSquash(strat: pytket.transform.PauliSynthStrat = PauliSynthStrat.Sets, cx_config: pytket.circuit.CXConfigType = CXConfigType.Snake) pytket.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.passes.PeepholeOptimise2Q(allow_swaps: bool = True) pytket.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.passes.PlacementPass(placer: pytket.placement.Placement) pytket.passes.BasePass#
Parameters:

placer – The Placement used for relabelling.

Returns:

a pass to relabel Circuit Qubits to Architecture Nodes

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

Overloaded function.

  1. RebaseCustom(gateset: set[pytket.circuit.OpType], cx_replacement: pytket.circuit.Circuit, tk1_replacement: Callable[[typing.Union[sympy.Expr, float], typing.Union[sympy.Expr, float], typing.Union[sympy.Expr, float]], pytket.circuit.Circuit]) -> pytket.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, Reset and Collapse 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, Reset and Collapse)

  1. RebaseCustom(gateset: set[pytket.circuit.OpType], tk2_replacement: Callable[[typing.Union[sympy.Expr, float], typing.Union[sympy.Expr, float], typing.Union[sympy.Expr, float]], pytket.circuit.Circuit], tk1_replacement: Callable[[typing.Union[sympy.Expr, float], typing.Union[sympy.Expr, float], typing.Union[sympy.Expr, float]], pytket.circuit.Circuit]) -> pytket.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, Reset and Collapse operations are 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, Reset and Collapse)

pytket.passes.RebaseTket() pytket.passes.BasePass#

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

pytket.passes.RemoveBarriers() pytket.passes.BasePass#

A pass to remove all barrier instructions from the circuit.

pytket.passes.RemoveDiscarded() pytket.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.passes.RemoveImplicitQubitPermutation() pytket.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.passes.RemoveRedundancies() pytket.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.passes.RenameQubitsPass(qubit_map: dict[pytket.unit_id.Qubit, pytket.unit_id.Qubit]) pytket.passes.BasePass#

Rename some or all qubits.

Parameters:

qubit_map – map from old to new qubit names

pytket.passes.RoundAngles(n: int, only_zeros: bool = False) pytket.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.passes.RoutingPass(arc: pytket.architecture.Architecture) pytket.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.passes.SimplifyInitial(allow_classical: bool = True, create_all_qubits: bool = False, remove_redundancies: bool = True, xcirc: pytket.circuit.Circuit | None = None) pytket.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.passes.SimplifyMeasured() pytket.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.passes.SquashCustom(singleqs: set[pytket.circuit.OpType], tk1_replacement: Callable[[Union[sympy.Expr, float], Union[sympy.Expr, float], Union[sympy.Expr, float]], pytket.circuit.Circuit], always_squash_symbols: bool = False) pytket.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.passes.SquashRzPhasedX() pytket.passes.BasePass#

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

pytket.passes.SquashTK1() pytket.passes.BasePass#

Squash sequences of single-qubit gates to TK1 gates.

pytket.passes.SynthesiseOQC() pytket.passes.BasePass#

Optimises and converts all gates to ECR, Rz, SX and Phase. DEPRECATED: will be removed after pytket 1.28.

pytket.passes.SynthesiseTK() pytket.passes.BasePass#

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

pytket.passes.SynthesiseTket() pytket.passes.BasePass#

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

pytket.passes.SynthesiseUMD() pytket.passes.BasePass#

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

pytket.passes.ThreeQubitSquash(allow_swaps: bool = True) pytket.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.passes.ZXGraphlikeOptimisation() pytket.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.passes.ZZPhaseToRz() pytket.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: list[pytket.passes.BasePass], score_func: Callable[[Circuit], int])[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: list[pytket.passes.BasePass], score_func: Callable[[Circuit], int])[source]#

Constructs a PassSelector

Parameters:
  • passlist – list of pytket compilation passes

  • score_func – function to score the results of the compilation (lower scores are preferred)

apply(circ: Circuit) Circuit[source]#

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

Parameters:

circ – Circuit that should be compiled

Returns:

compiled circuit

get_scores() list[Optional[int]][source]#
Returns:

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

pytket.passes.script#

pytket.passes.script.compilation_pass_from_script(script: str) BasePass[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 – specification of pass

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

Formal grammar for specifying compilation passes.

This is the grammar assumed by complilation_pass_from_script().

Returns:

grammar in extended Backus–Naur form

pytket.passes.auto_rebase#

pytket.passes.auto_rebase.auto_rebase_pass(gateset: Set[OpType], allow_swaps: bool = False) BasePass[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, Reset and Collapse 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:

RebaseCustom

pytket.passes.auto_rebase.auto_squash_pass(gateset: Set[OpType]) BasePass[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:

SquashCustom