Compiler: Converting to a Gateset

In this example, we show how to transpile a circuit to contain only a device’s native gateset. This native gateset is defined using a Config object which, below, contains only rotations about \(Z\) or \(X\) and a CNOT gate. This example contains a relatively simple configuration; they can generally be more complex.

import trueq as tq

# Define a device configuration
config = tq.Config(
    """
    Name: Example
    Mode: ZXZXZ
    Gate Z:
        Hamiltonian:
        - ['Z', 'phi']
    Gate X:
        Hamiltonian:
        - ['X', 'phi']
    Gate CNOT:
        Matrix:
        - [1, 0, 0, 0]
        - [0, 1, 0, 0]
        - [0, 0, 0, 1]
        - [0, 0, 1, 0]
    """
)

Next, we define a compiler based on the Config object we created above. Note that in this instance, we do not specify patterns for the transpiler so it will use the DEFAULT_PATTERNS, which are given in compilation. If this compiler is applied to a circuit, it will translate that circuit into a new circuit composed of gates contained in the config, so that it can be implemented on our imaginary device. The new circuit will be simplified according to DEFAULT_PATTERNS.

# Make a transpiler based on the device configuration, with default patterns:
transpiler = tq.compilation.get_transpiler(config)

Consider the following circuit that contains the non-native SWAP gate.

# Define a circuit
circuit = tq.Circuit([{(0, 1): tq.Gate.swap}])
circuit
Circuit
Key:
No key present in circuit.
 
imm
(0, 1): Gate.swap
Name:
  • Gate.swap
Aliases:
  • Gate.swap
Likeness:
  • SWAP
Generators:
  • 'XX': 90.0
  • 'YY': 90.0
  • 'ZZ': 90.0
Matrix:
  • 1.00 1.00 1.00 1.00


Using the transpiler, we can convert it into a new circuit. Notice that all of the gates in the new circuit are from the Config object, and that single-qubit gates are decomposed into sequences of the form \(Z(\theta)X(90)Z(\phi)X(90)Z(\gamma)\), which is consistent with the mode in the configuration. Hovering your cursor over each gate will display the rotation angle.

transpiled_circuit = transpiler.compile(circuit)
transpiled_circuit
Circuit
Key:
No key present in circuit.
   
 
(0): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = -239.41102
Generators:
  • 'Z': -239.411
Matrix:
  • -0.50 0.87j -0.50 -0.87j
(1): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = 60.930152
Generators:
  • 'Z': 60.93
Matrix:
  • 0.86 -0.51j 0.86 0.51j
 
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
(0): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = 71.804152
Generators:
  • 'Z': 71.804
Matrix:
  • 0.81 -0.59j 0.81 0.59j
(1): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = 93.416259
Generators:
  • 'Z': 93.416
Matrix:
  • 0.69 -0.73j 0.69 0.73j
 
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
(0): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = -259.336792
Generators:
  • 'Z': -259.337
Matrix:
  • -0.64 0.77j -0.64 -0.77j
(1): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = -141.342409
Generators:
  • 'Z': -141.342
Matrix:
  • 0.33 0.94j 0.33 -0.94j
imm
(0, 1): Example.CNOT()
Name:
  • Example.CNOT
Aliases:
  • Gate.cx
  • Gate.cnot
Likeness:
  • CNOT
Generators:
  • 'IX': 90.0
  • 'ZI': 90.0
  • 'ZX': -90.0
Matrix:
  • 0.71 -0.71j 0.71 -0.71j 0.71 -0.71j 0.71 -0.71j
 
 
(0): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = -269.090448
Generators:
  • 'Z': -269.09
Matrix:
  • -0.70 0.71j -0.70 -0.71j
(1): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = -189.114264
Generators:
  • 'Z': -189.114
Matrix:
  • -0.08 1.00j -0.08 -1.00j
 
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
(0): Example.Z(phi)
Name:
  • Example.Z
Aliases:
  • Gate.s
  • Gate.cliff9
Parameters:
  • phi = 90.0
Generators:
  • 'Z': 90.0
Matrix:
  • 0.71 -0.71j 0.71 0.71j
(1): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = 92.388201
Generators:
  • 'Z': 92.388
Matrix:
  • 0.69 -0.72j 0.69 0.72j
 
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
(0): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = 39.518011
Generators:
  • 'Z': 39.518
Matrix:
  • 0.94 -0.34j 0.94 0.34j
(1): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = -194.560238
Generators:
  • 'Z': -194.56
Matrix:
  • -0.13 0.99j -0.13 -0.99j
imm
(0, 1): Example.CNOT()
Name:
  • Example.CNOT
Aliases:
  • Gate.cx
  • Gate.cnot
Likeness:
  • CNOT
Generators:
  • 'IX': 90.0
  • 'ZI': 90.0
  • 'ZX': -90.0
Matrix:
  • 0.71 -0.71j 0.71 -0.71j 0.71 -0.71j 0.71 -0.71j
 
 
(0): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = -39.518011
Generators:
  • 'Z': -39.518
Matrix:
  • 0.94 0.34j 0.94 -0.34j
(1): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = -38.877504
Generators:
  • 'Z': -38.878
Matrix:
  • 0.94 0.33j 0.94 -0.33j
 
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
(0): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = 89.997853
Generators:
  • 'Z': 89.998
Matrix:
  • 0.71 -0.71j 0.71 0.71j
(1): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = 95.944974
Generators:
  • 'Z': 95.945
Matrix:
  • 0.67 -0.74j 0.67 0.74j
 
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
(0): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = 87.929141
Generators:
  • 'Z': 87.929
Matrix:
  • 0.72 -0.69j 0.72 0.69j
(1): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = -187.319496
Generators:
  • 'Z': -187.319
Matrix:
  • -0.06 1.00j -0.06 -1.00j
imm
(0, 1): Example.CNOT()
Name:
  • Example.CNOT
Aliases:
  • Gate.cx
  • Gate.cnot
Likeness:
  • CNOT
Generators:
  • 'IX': 90.0
  • 'ZI': 90.0
  • 'ZX': -90.0
Matrix:
  • 0.71 -0.71j 0.71 -0.71j 0.71 -0.71j 0.71 -0.71j
 
 
(0): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = -229.513417
Generators:
  • 'Z': -229.513
Matrix:
  • -0.42 0.91j -0.42 -0.91j
(1): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = -176.922356
Generators:
  • 'Z': -176.922
Matrix:
  • 0.03 1.00j 0.03 -1.00j
 
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
(0): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = 95.804763
Generators:
  • 'Z': 95.805
Matrix:
  • 0.67 -0.74j 0.67 0.74j
(1): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = 68.752907
Generators:
  • 'Z': 68.753
Matrix:
  • 0.83 -0.56j 0.83 0.56j
 
(0): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
(1): Example.X(phi)
Name:
  • Example.X
Aliases:
  • Gate.cliff5
Parameters:
  • phi = 90
Generators:
  • 'X': 90.0
Matrix:
  • 0.71 -0.71j -0.71j 0.71
 
(0): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = -229.401977
Generators:
  • 'Z': -229.402
Matrix:
  • -0.42 0.91j -0.42 -0.91j
(1): Example.Z(phi)
Name:
  • Example.Z
Parameters:
  • phi = -78.894047
Generators:
  • 'Z': -78.894
Matrix:
  • 0.77 0.64j 0.77 -0.64j


We can verify that this circuit makes the SWAP gate using an ideal simulator:

matrix = tq.Simulator().operator(transpiled_circuit).mat()

print("Process Infidelity: ", tq.math.proc_infidelity(matrix, tq.Gate.swap.mat))

# Note that the compiler does not preserve global phase, so there be a complex global
# constant offset from the ideal matrix.
tq.visualization.plot_mat(matrix)
compiler

Out:

Process Infidelity:  3.7050007506422844e-10

Total running time of the script: ( 0 minutes 0.461 seconds)

Gallery generated by Sphinx-Gallery