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: YZXY
  • 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.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.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.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.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.cliff8
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 = 22.0
Generators:
  • 'Z': 22.0
Matrix:
  • 0.98 -0.19j 0.98 0.19j
(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.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.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.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.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.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.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.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.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.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.s
  • Gate.sz
  • Gate.cliff9
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)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = -270.0
Generators:
  • 'Z': -270.0
Matrix:
  • -0.71 0.71j -0.71 -0.71j
(1): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -55.1887
Generators:
  • 'Z': -55.189
Matrix:
  • 0.89 0.46j 0.89 -0.46j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -14.999174
Generators:
  • 'Z': -14.999
Matrix:
  • 0.99 0.13j 0.99 -0.13j
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 11.934572
Generators:
  • 'Z': 11.935
Matrix:
  • 0.99 -0.10j 0.99 0.10j
 
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 = 177.666673
Generators:
  • 'Z': 177.667
Matrix:
  • 0.02 -1.00j 0.02 1.00j
(1): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 158.824996
Generators:
  • 'Z': 158.825
Matrix:
  • 0.18 -0.98j 0.18 0.98j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 128.113916
Generators:
  • 'Z': 128.114
Matrix:
  • 0.44 -0.90j 0.44 0.90j
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 20.053504
Generators:
  • 'Z': 20.054
Matrix:
  • 0.98 -0.17j 0.98 0.17j
 
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 = 68.825271
Generators:
  • 'Z': 68.825
Matrix:
  • 0.82 -0.57j 0.82 0.57j
(1): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = -270.0
Generators:
  • 'Z': -270.0
Matrix:
  • -0.71 0.71j -0.71 -0.71j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 12.622546
Generators:
  • 'Z': 12.623
Matrix:
  • 0.99 -0.11j 0.99 0.11j
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 42.234873
Generators:
  • 'Z': 42.235
Matrix:
  • 0.93 -0.36j 0.93 0.36j
 
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)
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)
Parameters:
  • phi = -151.28525
Generators:
  • 'Z': -151.285
Matrix:
  • 0.25 0.97j 0.25 -0.97j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -179.99991
Generators:
  • 'Z': -180.0
Matrix:
  • 1.00j -1.00j
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -180.000101
Generators:
  • 'Z': -180.0
Matrix:
  • 1.00j -1.00j
 
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)
Aliases:
  • Gate.id
  • Gate.i
  • Gate.cliff0
Likeness:
  • Identity
Parameters:
  • phi = 0.0
Generators:
  • 'I': 0
Matrix:
  • 1.00 1.00
(1): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 82.502861
Generators:
  • 'Z': 82.503
Matrix:
  • 0.75 -0.66j 0.75 0.66j
(2): z(phi)
Name:
  • z(phi)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = -270.0
Generators:
  • 'Z': -270.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 = -270.0
Generators:
  • 'Z': -270.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)
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)
Parameters:
  • phi = -28.715197
Generators:
  • 'Z': -28.715
Matrix:
  • 0.97 0.25j 0.97 -0.25j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -4.8e-05
Generators:
  • 'Z': -0.0
Matrix:
  • 1.00 1.00
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -4e-05
Generators:
  • 'Z': -0.0
Matrix:
  • 1.00 1.00
 
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 = 21.175025
Generators:
  • 'Z': 21.175
Matrix:
  • 0.98 -0.18j 0.98 0.18j
(1): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -233.289772
Generators:
  • 'Z': -233.29
Matrix:
  • -0.45 0.89j -0.45 -0.89j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -54.170726
Generators:
  • 'Z': -54.171
Matrix:
  • 0.89 0.46j 0.89 -0.46j
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -9.139392
Generators:
  • 'Z': -9.139
Matrix:
  • 1.00 0.08j 1.00 -0.08j
 
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)
Aliases:
  • Gate.s
  • Gate.sz
  • Gate.cliff9
Parameters:
  • phi = -270.0
Generators:
  • 'Z': -270.0
Matrix:
  • -0.71 0.71j -0.71 -0.71j
(1): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 72.748349
Generators:
  • 'Z': 72.748
Matrix:
  • 0.81 -0.59j 0.81 0.59j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 34.96777
Generators:
  • 'Z': 34.968
Matrix:
  • 0.95 -0.30j 0.95 0.30j
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = 129.668447
Generators:
  • 'Z': 129.668
Matrix:
  • 0.43 -0.91j 0.43 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 = -267.666673
Generators:
  • 'Z': -267.667
Matrix:
  • -0.69 0.72j -0.69 -0.72j
(1): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -145.530395
Generators:
  • 'Z': -145.53
Matrix:
  • 0.30 0.96j 0.30 -0.96j
(2): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -243.705736
Generators:
  • 'Z': -243.706
Matrix:
  • -0.53 0.85j -0.53 -0.85j
(3): z(phi)
Name:
  • z(phi)
Parameters:
  • phi = -184.112297
Generators:
  • 'Z': -184.112
Matrix:
  • -0.04 1.00j -0.04 -1.00j
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.