Download

Download this file as Jupyter notebook: custom.ipynb.

Example: Defining Custom Compilers

Building a general compiler which is aware of all possible simplification rules would result in an overly complex and rigid tool, and would be difficult to generalize for all hardware implementations. By leaving the compiler itself unadorned and allowing for custom rules, very complex compilation instructions can be expressed in a simple and readable fashion.

With this in mind, True-Q™'s Compiler works by applying an ordered list of built-in and/or user-specified Passes to a circuit in order. Pass objects define rules for how to decompose, replace, or remove Gates (or more generally, Operations), while possibly also adding or removing Cycles.

Getting started

Let’s start with a simple example that demonstrates the actions of a compiler. Consider the circuit below:

[2]:
import trueq as tq

circuit = tq.Circuit(
    cycles=[
        tq.Cycle({(0, 1): tq.Gate.cz}),
        tq.Cycle({0: tq.Gate.h, 1: tq.Gate.h}),
        tq.Cycle({(2, 1): tq.Gate.cx}),
        tq.Cycle({(0, 1): tq.Gate.cy, 2: tq.Gate.z}),
    ]
)
circuit.draw()
[2]:
0 1 2 Key: Labels: (0, 1) Name: Gate.cz Aliases: Gate.cz Locally Equivalent: CNOT Generators: ZZ: -90.00 ZI: 90.00 IZ: 90.00 1.00 1.00 1.00 -1.00 CZ CZ Labels: (0,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H Labels: (1,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H Labels: (2, 1) Name: Gate.cx Aliases: Gate.cx Gate.cnot Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0, 1) Name: Gate.cy Aliases: Gate.cy Locally Equivalent: CNOT Generators: ZY: -90.00 IY: 90.00 ZI: 90.00 1.00 1.00 -1.00j 1.00j CY CY Labels: (2,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z

We want to define a compiler that can replace certain cycles within this circuit with different cycles, and furthermore merge any resulting additional single- and two-qudit operations. To do that, we define two types of passes: a CycleReplacement and a Merge pass:

[3]:
replace_pass = tq.compilation.CycleReplacement(
    target=tq.Cycle({(0, 1): tq.Gate.cz}),
    replacement=3 * [tq.Cycle({(0, 1): tq.Gate.cz})],
)
merge_pass = tq.compilation.Merge(max_sys=2)

The CycleReplacement replaces any occurrence of the target cycle with the replacement cycle. In this case, our replace_pass will replace tq.Gate.cz on qubits (0, 1) with three consecutive tq.Gate.cz on qubits (0, 1). The Merge pass looks for any neighbouring gates which act on max_sys qudits with the same labels and merge them into a single gate.

To see how those passes work in practice, let’s define two different compilers: one which applies only replace_pass and one which applies only merge_pass.

[4]:
replace_compiler = tq.compilation.Compiler(passes=[replace_pass])
merge_compiler = tq.compilation.Compiler(passes=[merge_pass])

Here is the output from the first compiler:

[5]:
compilation1 = replace_compiler.compile(circuit)
compilation1.draw()
[5]:
0 1 2 Key: Labels: (0, 1) Name: Gate.cz Aliases: Gate.cz Locally Equivalent: CNOT Generators: ZZ: -90.00 ZI: 90.00 IZ: 90.00 1.00 1.00 1.00 -1.00 CZ CZ Labels: (0, 1) Name: Gate.cz Aliases: Gate.cz Locally Equivalent: CNOT Generators: ZZ: -90.00 ZI: 90.00 IZ: 90.00 1.00 1.00 1.00 -1.00 CZ CZ Labels: (0, 1) Name: Gate.cz Aliases: Gate.cz Locally Equivalent: CNOT Generators: ZZ: -90.00 ZI: 90.00 IZ: 90.00 1.00 1.00 1.00 -1.00 CZ CZ Labels: (0,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H Labels: (1,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H Labels: (2, 1) Name: Gate.cx Aliases: Gate.cx Gate.cnot Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0, 1) Name: Gate.cy Aliases: Gate.cy Locally Equivalent: CNOT Generators: ZY: -90.00 IY: 90.00 ZI: 90.00 1.00 1.00 -1.00j 1.00j CY CY Labels: (2,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z

Notice that the compiler constructed with replace_pass replaced the tq.Gate.cz with three consecutive tq.Gate.cz gates on qubits (0, 1), as expected.

Now let’s apply our merge_compiler to this circuit:

[6]:
compilation2 = merge_compiler.compile(compilation1)
compilation2.draw()
[6]:
0 1 2 Key: Labels: (0, 1) Name: Gate Locally Equivalent: CNOT Generators: YY: 90.00 XX: 90.00 ZZ: 90.00 YI: -69.28 IY: -69.28 YZ: 69.28 YX: 69.28 ... 0.50 0.50 0.50 -0.50 0.50 -0.50 0.50 0.50 0.50 0.50 -0.50 0.50 0.50 -0.50 -0.50 -0.50 Labels: (2, 1) Name: Gate Locally Equivalent: CNOT Generators: ZX: 90.00 IX: -90.00 ZI: 90.00 1.00 1.00 -1.00 -1.00 Labels: (0, 1) Name: Gate.cy Aliases: Gate.cy Locally Equivalent: CNOT Generators: ZY: -90.00 IY: 90.00 ZI: 90.00 1.00 1.00 -1.00j 1.00j CY CY

Note that the compiler constructed using the merge_pass merged the first four cycles into a single cycle, as intended.

Applying first our replace_compiler and then our merge_compiler has the effect of the cycle replacement pass and the cycle merging pass to be executed in that order.

The same can be achieved through a single compiler that we initialize with a list containing both passes:

[7]:
composite_compiler = tq.compilation.Compiler(passes=[replace_pass, merge_pass])

composite_compilation = composite_compiler.compile(circuit)
composite_compilation.draw()
[7]:
0 1 2 Key: Labels: (0, 1) Name: Gate Locally Equivalent: CNOT Generators: YY: 90.00 XX: 90.00 ZZ: 90.00 YI: -69.28 IY: -69.28 YZ: 69.28 YX: 69.28 ... 0.50 0.50 0.50 -0.50 0.50 -0.50 0.50 0.50 0.50 0.50 -0.50 0.50 0.50 -0.50 -0.50 -0.50 Labels: (2, 1) Name: Gate Locally Equivalent: CNOT Generators: ZX: 90.00 IX: -90.00 ZI: 90.00 1.00 1.00 -1.00 -1.00 Labels: (0, 1) Name: Gate.cy Aliases: Gate.cy Locally Equivalent: CNOT Generators: ZY: -90.00 IY: 90.00 ZI: 90.00 1.00 1.00 -1.00j 1.00j CY CY

Predefined Pass Lists

In addition to the passes shown above, there are several other useful pass classes available as members of the Compiler class, for example: HARDWARE_PASSES, SIMPLIFY_PASSES, RC_PASSES, and NATIVE2Q_PASSES. Click on the links to their documentation for further details on each.

Note that these pass classes need to be instantiated before they can be given to the compiler constructor. For certain types of advanced usage, directly invoking the constructor may be the preferred method. However, the compiler also has two static covenience methods to automate pass instantiation: from_config() and basic(). The first of these uses a Config object to pass relevant gate factories to each pass, and the second is a further specialization that auto-instantiates these factories based on a desired entangling gate and mode (which may not be relevant to all passes).

[8]:
# define a compiler to simplify circuits
compiler1 = tq.Compiler.basic(passes=tq.Compiler.SIMPLIFY_PASSES)

# define a compiler that does randomized compiling
compiler2 = tq.Compiler.basic(passes=tq.Compiler.RC_PASSES)

# define a compiler that decomposes two-qubit gates into the specified entangling gate,
# then randomly compiles, and then converts all gates into hardware compatable gates.
# for this example, we use the maximally entangling cross-resonance gate.
passes = (
    tq.Compiler.NATIVE2Q_PASSES + tq.Compiler.RC_PASSES + tq.Compiler.HARDWARE_PASSES
)
compiler3 = tq.Compiler.basic(entangler=tq.Gate.rp("ZX", 90), passes=passes)

We demonstrate the last of these compilers by defining the following circuit:

[9]:
circuit = tq.Circuit(
    [{range(4): tq.Gate.h}, {(0, 1): tq.Gate.rp("ZZ", 22), (2, 3): tq.Gate.cz}]
).measure_all()
circuit
[9]:
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
Circuit
Key:
No key present in circuit.
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Gate.h
Name:
  • Gate.h
Aliases:
  • Gate.h
  • Gate.f
  • Gate.cliff12
Generators:
  • 'X': 127.279
  • 'Z': 127.279
Matrix:
  • 0.71 0.71 0.71 -0.71
(1): Gate.h
Name:
  • Gate.h
Aliases:
  • Gate.h
  • Gate.f
  • Gate.cliff12
Generators:
  • 'X': 127.279
  • 'Z': 127.279
Matrix:
  • 0.71 0.71 0.71 -0.71
(2): Gate.h
Name:
  • Gate.h
Aliases:
  • Gate.h
  • Gate.f
  • Gate.cliff12
Generators:
  • 'X': 127.279
  • 'Z': 127.279
Matrix:
  • 0.71 0.71 0.71 -0.71
(3): Gate.h
Name:
  • Gate.h
Aliases:
  • Gate.h
  • Gate.f
  • Gate.cliff12
Generators:
  • 'X': 127.279
  • 'Z': 127.279
Matrix:
  • 0.71 0.71 0.71 -0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 1): Gate(ZZ)
Name:
  • Gate(ZZ)
Likeness:
  • Non-Clifford
Generators:
  • 'ZZ': 22.0
Matrix:
  • 0.98 -0.19j 0.98 0.19j 0.98 0.19j 0.98 -0.19j
(2, 3): Gate.cz
Name:
  • Gate.cz
Aliases:
  • Gate.cz
Likeness:
  • CNOT
Generators:
  • 'ZZ': -90.0
  • 'ZI': 90.0
  • 'IZ': 90.0
Matrix:
  • 1.00 1.00 1.00 -1.00
 
1
Marker 1
Compilation tools may only recompile cycles with equal markers.
(0): Meas()
Name:
  • Meas()
(1): Meas()
Name:
  • Meas()
(2): Meas()
Name:
  • Meas()
(3): Meas()
Name:
  • Meas()

We see that the compiled circuit contains only CNOT gates, Z rotations, and X90 rotations. Adjacent single qubit gates have been merged together on each qubit.

[10]:
compiler3.compile(circuit)
[10]:
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
Circuit
Key:
  • compiled_pauli: ZXYX
  • protocol: RC
  • twirl: Paulis on [0, 1, 2, 3]
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
(1): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
(2): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
(3): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(2): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(3): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
(1): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.z
  • Gate.cliff3
Parameters:
  • phi = 180.0
Generators:
  • 'Z': 180.0
Matrix:
  • -1.00j 1.00j
(2): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Parameters:
  • phi = 0.0
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
(3): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.z
  • Gate.cliff3
Parameters:
  • phi = 180.0
Generators:
  • 'Z': 180.0
Matrix:
  • -1.00j 1.00j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(2): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(3): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.z
  • Gate.cliff3
Parameters:
  • phi = 180.0
Generators:
  • 'Z': 180.0
Matrix:
  • -1.00j 1.00j
(1): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
(2): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
(3): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
2
Marker 2
Compilation tools may only recompile cycles with equal markers.
(0, 1): entangler()
Name:
  • entangler()
Likeness:
  • CNOT
Generators:
  • 'ZX': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71 0.71 0.71j 0.71j 0.71
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
(1): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
(2): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Parameters:
  • phi = 0.0
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
(3): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(2): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(3): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 158.0
Generators:
  • 'Z': 158.0
Matrix:
  • 0.19 -0.98j 0.19 0.98j
(1): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Parameters:
  • phi = 0.0
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
(2): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
(3): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Parameters:
  • phi = 0.0
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(2): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(3): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
(1): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
(2): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Parameters:
  • phi = 0.0
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
(3): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
3
Marker 3
Compilation tools may only recompile cycles with equal markers.
(0, 1): entangler()
Name:
  • entangler()
Likeness:
  • CNOT
Generators:
  • 'ZX': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71 0.71 0.71j 0.71j 0.71
(2, 3): entangler()
Name:
  • entangler()
Likeness:
  • CNOT
Generators:
  • 'ZX': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71 0.71 0.71j 0.71j 0.71
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
(1): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.z
  • Gate.cliff3
Parameters:
  • phi = 180.0
Generators:
  • 'Z': 180.0
Matrix:
  • -1.00j 1.00j
(2): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
(3): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.z
  • Gate.cliff3
Parameters:
  • phi = 180.0
Generators:
  • 'Z': 180.0
Matrix:
  • -1.00j 1.00j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(2): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(3): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
(1): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
(2): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Parameters:
  • phi = 0.0
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
(3): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(2): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(3): sx()
Name:
  • sx()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
(1): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Parameters:
  • phi = 0.0
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
(2): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Parameters:
  • phi = 0.0
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
(3): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.cliff8
Parameters:
  • phi = -90.0
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
1
Marker 1
Compilation tools may only recompile cycles with equal markers.
(0): Meas()
Name:
  • Meas()
(1): Meas()
Name:
  • Meas()
(2): Meas()
Name:
  • Meas()
(3): Meas()
Name:
  • Meas()

Custom Pass Lists

Custom lists of passes can be used if none of the predifined lists have the desired behaviour. We can pass lists of these classes to from_config() or basic(), as discussed above, or we can use the compiler constructor directly, as in the following example.

First, we define a device configuration. Here, we use the Berkeley gate as the entangling operation.

[11]:
b = tq.Gate.from_generators("XX", 90, "YY", 45)
config = tq.Config(
    factories=[
        tq.config.GateFactory.from_matrix("B", b),
        tq.config.GateFactory.from_hamiltonian("x90", [["X", 90]]),
        tq.config.GateFactory.from_hamiltonian("z", [["Z", "phi"]]),
    ],
    mode="ZXZXZ",
)

Next, we define a compiler. Unlike, HARDWARE_PASSES, this compiler starts by merging adjacent two-qubit gates. Also, we know that any two qubit gate can be decomposed into two Berkeley gates interleaved with single qubit gates. We use tq.compilation.NativeDecomp fixed at depth=2 to force every two-qubit gate to decompose into two Berkeley gates, even if only one is required.

[12]:
decomposer = tq.compilation.NativeDecomp(depth=2, factories=config.factories)
compiler = tq.Compiler(
    [
        tq.compilation.Merge(max_sys=2),
        tq.compilation.Parallel(decomposer),
        tq.compilation.Merge(),
        tq.compilation.Native1Q(config.factories),
        tq.compilation.RemoveEmptyCycle(),
    ]
)

Next, we define a circuit to test this compiler on.

[13]:
circuit = tq.Circuit(
    [
        {range(4): tq.Gate.h},
        {(0, 1): tq.Gate.rp("ZZ", 22)},
        {(0, 1): tq.Gate.cnot, (2, 3): tq.Gate.swap},
    ]
).measure_all()
circuit
[13]:
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
Circuit
Key:
No key present in circuit.
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Gate.h
Name:
  • Gate.h
Aliases:
  • Gate.h
  • Gate.f
  • Gate.cliff12
Generators:
  • 'X': 127.279
  • 'Z': 127.279
Matrix:
  • 0.71 0.71 0.71 -0.71
(1): Gate.h
Name:
  • Gate.h
Aliases:
  • Gate.h
  • Gate.f
  • Gate.cliff12
Generators:
  • 'X': 127.279
  • 'Z': 127.279
Matrix:
  • 0.71 0.71 0.71 -0.71
(2): Gate.h
Name:
  • Gate.h
Aliases:
  • Gate.h
  • Gate.f
  • Gate.cliff12
Generators:
  • 'X': 127.279
  • 'Z': 127.279
Matrix:
  • 0.71 0.71 0.71 -0.71
(3): Gate.h
Name:
  • Gate.h
Aliases:
  • Gate.h
  • Gate.f
  • Gate.cliff12
Generators:
  • 'X': 127.279
  • 'Z': 127.279
Matrix:
  • 0.71 0.71 0.71 -0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 1): Gate(ZZ)
Name:
  • Gate(ZZ)
Likeness:
  • Non-Clifford
Generators:
  • 'ZZ': 22.0
Matrix:
  • 0.98 -0.19j 0.98 0.19j 0.98 0.19j 0.98 -0.19j
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 1): Gate.cx
Name:
  • Gate.cx
Aliases:
  • Gate.cx
  • Gate.cnot
Likeness:
  • CNOT
Generators:
  • 'ZX': -90.0
  • 'IX': 90.0
  • 'ZI': 90.0
Matrix:
  • 1.00 1.00 1.00 1.00
(2, 3): Gate.swap
Name:
  • Gate.swap
Aliases:
  • Gate.swap
Likeness:
  • SWAP
Generators:
  • 'YY': 90.0
  • 'XX': 90.0
  • 'ZZ': 90.0
Matrix:
  • 1.00 1.00 1.00 1.00
 
1
Marker 1
Compilation tools may only recompile cycles with equal markers.
(0): Meas()
Name:
  • Meas()
(1): Meas()
Name:
  • Meas()
(2): Meas()
Name:
  • Meas()
(3): Meas()
Name:
  • Meas()

The resulting compiled circuit is as follows.

[14]:
compiler.compile(circuit)
[14]:
True-Q formatting will not be loaded without trusting this notebook or rerunning the affected cells. Notebooks can be marked as trusted by clicking "File -> Trust Notebook".
Circuit
Key:
No key present in circuit.
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -90.000296
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
(1): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -174.173705
Generators:
  • 'Z': -174.174
Matrix:
  • 0.05 1.00j 0.05 -1.00j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -40.245702
Generators:
  • 'Z': -40.246
Matrix:
  • 0.94 0.34j 0.94 -0.34j
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -227.834235
Generators:
  • 'Z': -227.834
Matrix:
  • -0.41 0.91j -0.41 -0.91j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(2): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(3): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 81.44741
Generators:
  • 'Z': 81.447
Matrix:
  • 0.76 -0.65j 0.76 0.65j
(1): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 110.237329
Generators:
  • 'Z': 110.237
Matrix:
  • 0.57 -0.82j 0.57 0.82j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 43.028779
Generators:
  • 'Z': 43.029
Matrix:
  • 0.93 -0.37j 0.93 0.37j
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 125.929159
Generators:
  • 'Z': 125.929
Matrix:
  • 0.45 -0.89j 0.45 0.89j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(2): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(3): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -159.762715
Generators:
  • 'Z': -159.763
Matrix:
  • 0.18 0.98j 0.18 -0.98j
(1): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -269.999999
Generators:
  • 'Z': -270.0
Matrix:
  • -0.71 0.71j -0.71 -0.71j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -9.388901
Generators:
  • 'Z': -9.389
Matrix:
  • 1.00 0.08j 1.00 -0.08j
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -165.428746
Generators:
  • 'Z': -165.429
Matrix:
  • 0.13 0.99j 0.13 -0.99j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 1): B()
Name:
  • B()
Likeness:
  • Non-Clifford
Generators:
  • 'YY': 45.0
  • 'XX': 90.0
Matrix:
  • 0.92 -0.38j 0.38 -0.92j -0.92j 0.38 -0.38j 0.92
(2, 3): B()
Name:
  • B()
Likeness:
  • Non-Clifford
Generators:
  • 'YY': 45.0
  • 'XX': 90.0
Matrix:
  • 0.92 -0.38j 0.38 -0.92j -0.92j 0.38 -0.38j 0.92
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 89.999999
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
(1): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 75.388876
Generators:
  • 'Z': 75.389
Matrix:
  • 0.79 -0.61j 0.79 0.61j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -180.00006
Generators:
  • 'Z': -180.0
Matrix:
  • 1.00j -1.00j
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -2.9e-05
Generators:
  • 'Z': -0.0
Matrix:
  • 1.00 1.00
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(2): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(3): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 179.999558
Generators:
  • 'Z': 180.0
Matrix:
  • -1.00j 1.00j
(1): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 151.684199
Generators:
  • 'Z': 151.684
Matrix:
  • 0.24 -0.97j 0.24 0.97j
(2): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
(3): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(2): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(3): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -90.000001
Generators:
  • 'Z': -90.0
Matrix:
  • 0.71 0.71j 0.71 -0.71j
(1): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -104.611124
Generators:
  • 'Z': -104.611
Matrix:
  • 0.61 0.79j 0.61 -0.79j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 3.3e-05
Generators:
  • 'Z': 0.0
Matrix:
  • 1.00 1.00
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -179.999962
Generators:
  • 'Z': -180.0
Matrix:
  • 1.00j -1.00j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 1): B()
Name:
  • B()
Likeness:
  • Non-Clifford
Generators:
  • 'YY': 45.0
  • 'XX': 90.0
Matrix:
  • 0.92 -0.38j 0.38 -0.92j -0.92j 0.38 -0.38j 0.92
(2, 3): B()
Name:
  • B()
Likeness:
  • Non-Clifford
Generators:
  • 'YY': 45.0
  • 'XX': 90.0
Matrix:
  • 0.92 -0.38j 0.38 -0.92j -0.92j 0.38 -0.38j 0.92
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 110.237329
Generators:
  • 'Z': 110.237
Matrix:
  • 0.57 -0.82j 0.57 0.82j
(1): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -182.021507
Generators:
  • 'Z': -182.022
Matrix:
  • -0.02 1.00j -0.02 -1.00j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -228.679067
Generators:
  • 'Z': -228.679
Matrix:
  • -0.41 0.91j -0.41 -0.91j
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -226.371518
Generators:
  • 'Z': -226.372
Matrix:
  • -0.39 0.92j -0.39 -0.92j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(2): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(3): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 89.999688
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
(1): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 95.465494
Generators:
  • 'Z': 95.465
Matrix:
  • 0.67 -0.74j 0.67 0.74j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 55.40994
Generators:
  • 'Z': 55.41
Matrix:
  • 0.89 -0.46j 0.89 0.46j
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 49.012957
Generators:
  • 'Z': 49.013
Matrix:
  • 0.91 -0.41j 0.91 0.41j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(2): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(3): x90()
Name:
  • x90()
Aliases:
  • Gate.sx
  • Gate.cliff5
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -188.552482
Generators:
  • 'Z': -188.552
Matrix:
  • -0.07 1.00j -0.07 -1.00j
(1): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -47.666171
Generators:
  • 'Z': -47.666
Matrix:
  • 0.91 0.40j 0.91 -0.40j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -197.829078
Generators:
  • 'Z': -197.829
Matrix:
  • -0.15 0.99j -0.15 -0.99j
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -206.892611
Generators:
  • 'Z': -206.893
Matrix:
  • -0.23 0.97j -0.23 -0.97j
1
Marker 1
Compilation tools may only recompile cycles with equal markers.
(0): Meas()
Name:
  • Meas()
(1): Meas()
Name:
  • Meas()
(2): Meas()
Name:
  • Meas()
(3): Meas()
Name:
  • Meas()

Download

Download this file as Jupyter notebook: custom.ipynb.