Download

Download this file as Jupyter notebook: synthesis.ipynb.

Example: Gate synthesis

This example demonstrates how the built-in compiler can be used to perform gate synthesis.

Synthesizing Single-Qubit Gates

We begin by generating a random gate in \(SU(2)\); we will synthesize this gate later.

[2]:
import trueq as tq

# create a Haar random SU(2) gate and print its matrix representation
U = tq.Gate.random(2)
U
[2]:
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".
Name:
  • Gate(Y, X, ...)
Generators:
  • 'Y': -14.7
  • 'X': 35.706
  • 'Z': -38.447
Matrix:
  • 0.85 -0.42j -0.13 -0.30j -0.30 -0.12j 0.38 -0.86j

We can perform single qubit gate decomposition into a number of possible “modes”. Here we decompose the gate U into a ZXZXZ decomposition, which is short hand for \(Z(\theta)X(90)Z(\phi)X(90)Z(\gamma)\). See QubitMode for a complete list of all available single qubit decompositions.

[3]:
synthesized_gate = tq.math.QubitMode.ZXZXZ.decompose(U)

# print the synthesized gate as a list of single-qubit rotations about Z and X
synthesized_gate
[3]:
[('Z', -87.59009451864381),
 ('X', 90),
 ('Z', 142.1405013235936),
 ('X', 90),
 ('Z', -132.3436393187573)]

Synthesizing Two-Qubit Gates

In the event that we want to express a two-qubit gate in terms of a different two-qubit gate, we can use the compiler to synthesize the desired gate. Here we decompose a random \(SU(4)\) operation so that it can be implemented using iSWAP gates.

[4]:
# define the gate to be synthesized
gate_to_be_synthesized = tq.Gate.random(4)

# re-express the gate using an iswap gate as the two-qubit gate
two_qubit_synthesized_gate = tq.math.decompose_unitary(
    target_gate=gate_to_be_synthesized, given_gate=tq.Gate.iswap
)

# print the synthesized gate
two_qubit_synthesized_gate
[4]:
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(Y, X, ...)
Name:
  • Gate(Y, X, ...)
Generators:
  • 'Y': 80.677
  • 'X': 90.152
  • 'Z': 88.193
Matrix:
  • 0.34 -0.52j -0.43 -0.65j 0.60 -0.50j 0.17 0.60j
(1): Gate(Y, X, ...)
Name:
  • Gate(Y, X, ...)
Generators:
  • 'Y': -153.361
  • 'X': -44.757
  • 'Z': 62.785
Matrix:
  • -0.34 -0.14j 0.43 -0.82j 0.08 0.92j 0.37
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 1): Gate.iswap
Name:
  • Gate.iswap
Aliases:
  • Gate.iswap
Likeness:
  • iSWAP
Generators:
  • 'YY': -90.0
  • 'XX': -90.0
Matrix:
  • 1.00 1.00j 1.00j 1.00
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Gate(Y, X, ...)
Name:
  • Gate(Y, X, ...)
Generators:
  • 'Y': 130.073
  • 'X': -10.045
  • 'Z': 34.247
Matrix:
  • 0.13 0.43j 0.15 -0.88j -0.28 0.85j -0.32 0.32j
(1): Gate(Y, X, ...)
Name:
  • Gate(Y, X, ...)
Generators:
  • 'Y': 3.946
  • 'X': -87.66
  • 'Z': -6.329
Matrix:
  • -0.06 -0.72j 0.69 -0.08j 0.68 -0.14j -0.16 -0.70j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 1): Gate.iswap
Name:
  • Gate.iswap
Aliases:
  • Gate.iswap
Likeness:
  • iSWAP
Generators:
  • 'YY': -90.0
  • 'XX': -90.0
Matrix:
  • 1.00 1.00j 1.00j 1.00
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Gate(Y, X, ...)
Name:
  • Gate(Y, X, ...)
Generators:
  • 'Y': 158.54
  • 'X': 19.566
  • 'Z': 13.754
Matrix:
  • 0.19 -0.02j -0.88 -0.44j 0.96 0.21j 0.13 0.14j
(1): Gate(Y, X, ...)
Name:
  • Gate(Y, X, ...)
Generators:
  • 'Y': 32.912
  • 'X': 104.051
  • 'Z': 55.007
Matrix:
  • 0.55 -0.30j -0.09 -0.78j 0.37 -0.69j 0.40 0.48j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 1): Gate.iswap
Name:
  • Gate.iswap
Aliases:
  • Gate.iswap
Likeness:
  • iSWAP
Generators:
  • 'YY': -90.0
  • 'XX': -90.0
Matrix:
  • 1.00 1.00j 1.00j 1.00
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Gate(Y, X, ...)
Name:
  • Gate(Y, X, ...)
Generators:
  • 'Y': 74.826
  • 'X': -30.386
  • 'Z': -133.451
Matrix:
  • 0.86 -0.03j 0.09 0.50j 0.28 -0.42j -0.78 -0.37j
(1): Gate(Y, X, ...)
Name:
  • Gate(Y, X, ...)
Generators:
  • 'Y': -56.334
  • 'X': 33.957
  • 'Z': 93.437
Matrix:
  • -0.28 -0.83j 0.02 -0.48j -0.43 0.21j 0.87 -0.08j

This circuit can be verified to reproduce the original random unitary using an ideal simulator:

[5]:
matrix = tq.Simulator().operator(two_qubit_synthesized_gate).mat()

# This will result in an identity gate up to a global complex phase.
tq.plot_mat(matrix @ gate_to_be_synthesized.adj.mat)
../../_images/guides_compilation_synthesis_8_0.png

Download

Download this file as Jupyter notebook: synthesis.ipynb.