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.

import trueq as tq

# create a Haar random SU(2) gate and print its matrix representation
U = tq.Gate.random(2)
U
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(X, Y, ...)
Generators:
  • 'X': 108.048
  • 'Y': -43.196
  • 'Z': 90.601
Matrix:
  • 0.64 -0.11j 0.74 -0.17j 0.41 -0.63j -0.32 0.57j


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.

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

Out:

[('Z', -3.609059736668513), ('X', 90), ('Z', 81.51778812800691), ('X', 90), ('Z', -47.190801728080466)]

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.

# 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_su4(
    target_gate=gate_to_be_synthesized, given_gate=tq.Gate.iswap
)

# print the synthesized gate
two_qubit_synthesized_gate

Out:

Warning: decompose_su4() was deprecated in version 2.9.0 and will be removed no earlier than version 2.10.0. decompose_su4() was renamed to decompose_unitary().
         (/home/user/jenkins/workspace/release trueq/docs/examples/compilation/synthesis.py:51)
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(X, Y, ...)
Name:
  • Gate(X, Y, ...)
Generators:
  • 'X': 60.114
  • 'Y': -101.645
  • 'Z': -156.435
Matrix:
  • 0.60 0.54j 0.01 -0.60j -0.53 0.28j -0.74 -0.30j
(1): Gate(X, Y, ...)
Name:
  • Gate(X, Y, ...)
Generators:
  • 'X': 54.317
  • 'Y': -102.441
  • 'Z': -43.067
Matrix:
  • 0.56 0.05j 0.46 -0.69j -0.83 0.01j 0.27 -0.49j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 1): Gate.iswap
Name:
  • Gate.iswap
Aliases:
  • Gate.iswap
Likeness:
  • iSWAP
Generators:
  • 'XX': -90.0
  • 'YY': -90.0
Matrix:
  • 1.00 1.00j 1.00j 1.00
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Gate(X, Y, ...)
Name:
  • Gate(X, Y, ...)
Generators:
  • 'X': 18.394
  • 'Y': 147.54
  • 'Z': -161.665
Matrix:
  • 0.08 0.77j -0.58 0.27j 0.49 -0.40j -0.66 -0.40j
(1): Gate(X, Y, ...)
Name:
  • Gate(X, Y, ...)
Generators:
  • 'X': -150.22
  • 'Y': 76.015
  • 'Z': 69.569
Matrix:
  • -0.38 -0.00j 0.80 0.47j 0.85 -0.37j 0.38 0.04j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 1): Gate.iswap
Name:
  • Gate.iswap
Aliases:
  • Gate.iswap
Likeness:
  • iSWAP
Generators:
  • 'XX': -90.0
  • 'YY': -90.0
Matrix:
  • 1.00 1.00j 1.00j 1.00
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Gate(X, Y, ...)
Name:
  • Gate(X, Y, ...)
Generators:
  • 'X': 159.703
  • 'Y': 22.582
  • 'Z': 35.592
Matrix:
  • -0.22 -0.11j -0.95 0.20j -0.97 -0.07j 0.20 -0.14j
(1): Gate(X, Y, ...)
Name:
  • Gate(X, Y, ...)
Generators:
  • 'X': 11.986
  • 'Y': 112.283
  • 'Z': 100.446
Matrix:
  • -0.37 -0.58j -0.48 0.54j 0.36 -0.63j 0.67 0.18j
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0, 1): Gate.iswap
Name:
  • Gate.iswap
Aliases:
  • Gate.iswap
Likeness:
  • iSWAP
Generators:
  • 'XX': -90.0
  • 'YY': -90.0
Matrix:
  • 1.00 1.00j 1.00j 1.00
 
 
Marker 0
Compilation tools may only recompile cycles with equal markers.
(0): Gate(X, Y, ...)
Name:
  • Gate(X, Y, ...)
Generators:
  • 'X': -172.161
  • 'Y': -273.137
  • 'Z': -13.504
Matrix:
  • -0.95 -0.02j 0.26 0.18j -0.27 0.16j -0.95 -0.04j
(1): Gate(X, Y, ...)
Name:
  • Gate(X, Y, ...)
Generators:
  • 'X': 181.636
  • 'Y': 39.925
  • 'Z': -113.327
Matrix:
  • 0.26 0.53j -0.77 -0.24j -0.60 -0.54j -0.59 0.03j


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

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

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

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

Gallery generated by Sphinx-Gallery