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': 121.339
  • 'Y': 13.359
  • 'Z': -0.415
Matrix:
  • 0.46 -0.15j -0.37 -0.79j -0.19 -0.85j 0.46 -0.16j


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', -96.63453377491444), ('X', 90), ('Z', 57.928248867566566), ('X', 90), ('Z', -84.06887965772853)]

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': -57.111
  • 'Y': -86.167
  • 'Z': -80.929
Matrix:
  • -0.54 0.44j -0.37 0.62j -0.43 -0.58j 0.58 0.38j
(1): Gate(X, Y, ...)
Name:
  • Gate(X, Y, ...)
Generators:
  • 'X': 57.992
  • 'Y': 16.616
  • 'Z': 106.109
Matrix:
  • -0.63 -0.65j -0.43 0.02j -0.38 -0.21j 0.85 -0.30j
 
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': -93.711
  • 'Y': 35.469
  • 'Z': 95.745
Matrix:
  • 0.73 0.08j -0.68 0.02j -0.49 0.46j -0.46 0.57j
(1): Gate(X, Y, ...)
Name:
  • Gate(X, Y, ...)
Generators:
  • 'X': 125.755
  • 'Y': -96.171
  • 'Z': 24.154
Matrix:
  • 0.03 -0.23j -0.06 -0.97j -0.95 -0.20j 0.23
 
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': 61.76
  • 'Y': -24.709
  • 'Z': 20.724
Matrix:
  • 0.68 -0.49j -0.02 -0.54j -0.39 -0.38j 0.82 -0.18j
(1): Gate(X, Y, ...)
Name:
  • Gate(X, Y, ...)
Generators:
  • 'X': -164.419
  • 'Y': 67.656
  • 'Z': 22.932
Matrix:
  • -0.13 -0.01j 0.90 0.43j 0.94 -0.33j 0.13
 
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': -249.469
  • 'Y': -50.701
  • 'Z': -22.642
Matrix:
  • -0.47 0.40j 0.56 0.55j 0.31 0.73j -0.55 0.29j
(1): Gate(X, Y, ...)
Name:
  • Gate(X, Y, ...)
Generators:
  • 'X': 47.339
  • 'Y': -20.057
  • 'Z': 20.711
Matrix:
  • -0.76 -0.49j -0.40 0.17j -0.15 0.40j -0.52 -0.74j


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.070 seconds)

Gallery generated by Sphinx-Gallery