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': 31.021
  • 'X': 21.659
  • 'Z': -11.363
Matrix:
  • -0.48 0.82j 0.28 -0.16j 0.06 0.32j -0.30 0.90j

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', -150.97355189023048),
 ('X', 90),
 ('Z', 142.23035171003625),
 ('X', 90),
 ('Z', -40.81973036029411)]

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': -23.097
  • 'X': -102.161
  • 'Z': 53.17
Matrix:
  • 0.59 0.26j -0.56 0.51j -0.73 0.22j -0.08 0.64j
(1): Gate(Y, X, ...)
Name:
  • Gate(Y, X, ...)
Generators:
  • 'Y': -116.751
  • 'X': -69.812
  • 'Z': 267.773
Matrix:
  • -0.66 -0.72j 0.14 0.18j -0.22 0.04j -0.97 0.11j
 
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': 93.286
  • 'X': 28.554
  • 'Z': 11.393
Matrix:
  • -0.06 -0.66j -0.24 0.71j -0.19 -0.73j 0.11 -0.65j
(1): Gate(Y, X, ...)
Name:
  • Gate(Y, X, ...)
Generators:
  • 'Y': 77.796
  • 'X': 180.149
  • 'Z': 72.425
Matrix:
  • 0.14 -0.40j 0.49 -0.76j 0.89 -0.17j -0.42 -0.02j
 
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': 110.723
  • 'X': -20.13
  • 'Z': -204.494
Matrix:
  • 0.28 0.86j -0.22 0.37j 0.34 -0.26j -0.88 -0.19j
(1): Gate(Y, X, ...)
Name:
  • Gate(Y, X, ...)
Generators:
  • 'Y': -6.605
  • 'X': -47.058
  • 'Z': -43.573
Matrix:
  • -0.15 -0.91j 0.30 -0.26j 0.36 -0.16j -0.76 -0.52j
 
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': -25.821
  • 'X': -85.628
  • 'Z': 17.287
Matrix:
  • -0.31 0.64j -0.66 -0.24j -0.41 -0.57j -0.53 0.48j
(1): Gate(Y, X, ...)
Name:
  • Gate(Y, X, ...)
Generators:
  • 'Y': -189.821
  • 'X': 55.684
  • 'Z': -81.185
Matrix:
  • -0.35 0.31j 0.88 -0.11j -0.80 -0.38j -0.23 -0.41j

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.