Download

Download this file as Jupyter notebook: rc_kak.ipynb.

Example: Randomly compiling arbitrary multi-qubit gates

This example shows how to use the RCKak compiler pass to randomly compile circuits with non-Clifford multi-qubit gates. Randomizing and correction cycles can be applied around hard cycles in the circuit without the need to first convert all entangling gates into Clifford operations.

Generating randomly compiled circuits

Consider some circuit with alternating easy and hard cycles, where entangling gates are considered to be hard, and single-qubit gates are considered to be easy:

[2]:
import trueq as tq
import numpy as np
import trueq.compilation as tqc
from trueq.config import fsim_factory
from trueq import CircuitCollection

cycle1 = {1: tq.Gate.h, 4: tq.Gate.h}
cycle2 = {(0, 1): tq.Gate.random(4)}
cycle3 = {1: tq.Gate.t, 3: tq.Gate.random(2)}
cycle4 = tq.Cycle({(1, 3): fsim_factory(11, 13)})

circuit = tq.Circuit([cycle1, cycle2, cycle3, cycle4] * 2)
circuit += cycle1
circuit.measure_all()
circuit.draw()
[2]:
0 1 3 4 Key: Labels: (1,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H Labels: (4,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H Labels: (0, 1) Name: Gate Locally Equivalent: Non-Clifford Generators: XX: -96.33 YI: -73.03 ZX: -60.74 ZI: -60.28 ZY: -55.58 IZ: 47.46 YZ: -42.35 ... 0.19 -0.36j 0.04 0.61j 0.42 -0.34j 0.42j -0.41 -0.02j 0.19 0.53j 0.19 0.60j 0.16 -0.32j -0.47 0.09j -0.07 0.28j -0.35 -0.21j -0.72 0.07j 0.24 0.62j -0.16 0.45j -0.18 -0.34j 0.26 -0.33j Labels: (1,) Name: Gate.t Aliases: Gate.t Generators: Z: 45.00 1.00 0.71 0.71j T Labels: (3,) Name: Gate Generators: Z: 130.77 Y: -25.57 X: -10.00 0.50 -0.85j 0.17 0.09j -0.18 0.05j 0.29 0.94j Labels: (1, 3) Name: FSim Parameters: theta: 11.00 phi: 13.00 Locally Equivalent: Non-Clifford Generators: YY: 11.00 XX: 11.00 ZI: 6.50 ZZ: -6.50 IZ: 6.50 YX: -0.00 XY: 0.00 1.00 -0.06j 0.98 -0.06j -0.01 -0.19j -0.01 -0.19j 0.98 -0.06j 0.99 0.17j Labels: (1,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H Labels: (4,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H Labels: (0, 1) Name: Gate Locally Equivalent: Non-Clifford Generators: XX: -96.33 YI: -73.03 ZX: -60.74 ZI: -60.28 ZY: -55.58 IZ: 47.46 YZ: -42.35 ... 0.19 -0.36j 0.04 0.61j 0.42 -0.34j 0.42j -0.41 -0.02j 0.19 0.53j 0.19 0.60j 0.16 -0.32j -0.47 0.09j -0.07 0.28j -0.35 -0.21j -0.72 0.07j 0.24 0.62j -0.16 0.45j -0.18 -0.34j 0.26 -0.33j Labels: (1,) Name: Gate.t Aliases: Gate.t Generators: Z: 45.00 1.00 0.71 0.71j T Labels: (3,) Name: Gate Generators: Z: 130.77 Y: -25.57 X: -10.00 0.50 -0.85j 0.17 0.09j -0.18 0.05j 0.29 0.94j Labels: (1, 3) Name: FSim Parameters: theta: 11.00 phi: 13.00 Locally Equivalent: Non-Clifford Generators: YY: 11.00 XX: 11.00 ZI: 6.50 ZZ: -6.50 IZ: 6.50 YX: -0.00 XY: 0.00 1.00 -0.06j 0.98 -0.06j -0.01 -0.19j -0.01 -0.19j 0.98 -0.06j 0.99 0.17j Labels: (1,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H Labels: (4,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H 1 Labels: (0,) Name: Meas M Labels: (1,) Name: Meas M Labels: (3,) Name: Meas M Labels: (4,) Name: Meas M

This circuit contains non-Clifford entangling gates which are not handled by the randomly_compile() protocol by default. To randomly compile this circuit, one approach is to first decompose the circuit into a logically equivalent circuit using a specified Clifford entangler:

[3]:
rc_circuits = tq.randomly_compile(circuit, entangler=tq.Gate.cx)
rc_circuits[0].draw()
[3]:
0 1 3 4 Key: twirl: Paulis on [0, 1, 3, 4] protocol: RC Labels: (0,) Name: Gate Generators: X: -91.19 Y: -51.56 Z: -40.74 0.63 -0.05j 0.69 0.36j 0.05 0.77j 0.30 -0.56j Labels: (1,) Name: Gate Generators: X: -151.32 Z: 103.76 Y: -0.18 -0.49 -0.28j 0.69 0.45j 0.69 0.45j 0.46 0.33j Labels: (3,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y Labels: (4,) Name: Gate.sy Aliases: Gate.sy Gate.cliff7 Generators: Y: 90.00 0.71 -0.71 0.71 0.71 SY 2 Labels: (0, 1) Name: cx Aliases: Gate.cx Gate.cnot Parameters: Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate Generators: Z: -117.52 X: -117.52 Y: -18.69 0.70 -0.11j 0.70 -0.11j 0.70 0.11j -0.70 -0.11j Labels: (1,) Name: Gate Generators: Z: 136.57 0.93 0.37j -0.93 0.37j Labels: (3,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: -180.00 1.00 -1.00 Z Labels: (4,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X 3 Labels: (0, 1) Name: cx Aliases: Gate.cx Gate.cnot Parameters: Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate.sy Aliases: Gate.sy Gate.cliff7 Generators: Y: 90.00 0.71 -0.71 0.71 0.71 SY Labels: (1,) Name: Gate Generators: Z: -121.40 -0.87 0.49j 0.87 0.49j Labels: (3,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y Labels: (4,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID 4 Labels: (0, 1) Name: cx Aliases: Gate.cx Gate.cnot Parameters: Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate Generators: Z: -149.55 X: -118.15 Y: 64.55 -0.52 0.54j -0.56 0.34j -0.02 0.66j 0.21 -0.72j Labels: (1,) Name: Gate Generators: Y: -135.16 Z: -133.72 X: -17.85 -0.70 -0.01j -0.00 0.71j -0.18 -0.69j 0.68 -0.18j Labels: (3,) Name: Gate Generators: X: 186.12 Y: -71.55 Z: 15.17 -0.16 -0.09j 0.45 -0.87j -0.25 -0.95j -0.18 0.05j Labels: (4,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID 5 Labels: (1, 3) Name: cx Aliases: Gate.cx Gate.cnot Parameters: Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID Labels: (1,) Name: Gate Generators: Y: 74.14 X: 61.11 Z: -61.11 0.55 0.45j -0.55 -0.45j 0.55 -0.45j 0.55 -0.45j Labels: (3,) Name: Gate Generators: X: 179.71 Y: -10.20 1.00 0.06j 1.00 -0.06j Labels: (4,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: -180.00 1.00 -1.00 Z 6 Labels: (1, 3) Name: cx Aliases: Gate.cx Gate.cnot Parameters: Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y Labels: (1,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: X: -127.28 Z: -127.28 0.71j 0.71j 0.71j -0.71j H Labels: (3,) Name: Gate Generators: Y: -179.17 X: 17.25 1.00 -0.10j -1.00 -0.10j Labels: (4,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X 7 Labels: (1, 3) Name: cx Aliases: Gate.cx Gate.cnot Parameters: Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate Generators: Z: -98.59 Y: 81.34 X: 44.04 0.69 0.36j -0.63 0.05j 0.30 -0.56j -0.05 -0.77j Labels: (1,) Name: Gate Generators: Y: -143.79 X: 140.69 Z: 14.82 -0.16 0.13j -0.25 -0.95j -0.94 0.27j -0.03 0.20j Labels: (3,) Name: Gate Generators: Y: -74.53 Z: 74.53 X: -65.74 0.50 -0.50j 0.50 0.50j -0.56 0.43j 0.43 0.56j Labels: (4,) Name: Gate.cliff6 Aliases: Gate.cliff6 Generators: Y: 270.00 -0.71 -0.71 0.71 -0.71 6 8 Labels: (0, 1) Name: cx Aliases: Gate.cx Gate.cnot Parameters: Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate Generators: Z: 117.52 X: 117.52 Y: -18.69 -0.70 -0.11j -0.70 -0.11j -0.70 0.11j 0.70 -0.11j Labels: (1,) Name: Gate Generators: X: 167.23 Y: -66.59 0.93 0.37j 0.93 -0.37j Labels: (3,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID Labels: (4,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID 9 Labels: (0, 1) Name: cx Aliases: Gate.cx Gate.cnot Parameters: Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate.cliff13 Aliases: Gate.cliff13 Generators: X: 127.28 Z: -127.28 0.71j -0.71j -0.71j -0.71j 13 Labels: (1,) Name: Gate Generators: Z: 121.40 0.87 0.49j -0.87 0.49j Labels: (3,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y Labels: (4,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y 10 Labels: (0, 1) Name: cx Aliases: Gate.cx Gate.cnot Parameters: Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate Generators: Z: -149.55 X: -118.15 Y: 64.55 -0.52 0.54j -0.56 0.34j -0.02 0.66j 0.21 -0.72j Labels: (1,) Name: Gate Generators: X: -88.80 Y: 12.17 Z: -11.85 -0.00 0.71j -0.70 -0.01j -0.68 0.18j 0.18 0.69j Labels: (3,) Name: Gate Generators: X: -148.94 Y: -57.25 Z: 12.14 0.18 -0.05j 0.25 0.95j -0.45 0.87j 0.16 0.09j Labels: (4,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: -180.00 1.00 -1.00 Z 11 Labels: (1, 3) Name: cx Aliases: Gate.cx Gate.cnot Parameters: Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: -180.00 1.00 -1.00 Z Labels: (1,) Name: Gate Generators: Z: -77.30 X: 77.30 Y: 63.72 0.55 -0.45j -0.55 0.45j -0.55 -0.45j -0.55 -0.45j Labels: (3,) Name: Gate Generators: Y: -179.71 X: 10.20 1.00 -0.06j -1.00 -0.06j Labels: (4,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y 12 Labels: (1, 3) Name: cx Aliases: Gate.cx Gate.cnot Parameters: Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (1,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: X: -127.28 Z: -127.28 0.71j 0.71j 0.71j -0.71j H Labels: (3,) Name: Gate Generators: X: -179.17 Y: -17.25 -1.00 0.10j -1.00 -0.10j Labels: (4,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X 13 Labels: (1, 3) Name: cx Aliases: Gate.cx Gate.cnot Parameters: Locally Equivalent: CNOT Generators: ZX: -90.00 IX: 90.00 ZI: 90.00 1.00 1.00 1.00 1.00 CX CX Labels: (0,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y Labels: (1,) Name: Gate Generators: Y: -123.57 X: -123.57 Z: -7.74 0.05 0.04j 0.75 0.66j -0.66 0.75j 0.04 -0.05j Labels: (3,) Name: Gate Generators: Y: -74.53 Z: 74.53 X: -65.74 0.50 -0.50j 0.50 0.50j -0.56 0.43j 0.43 0.56j Labels: (4,) Name: Gate.sy Aliases: Gate.sy Gate.cliff7 Generators: Y: 90.00 0.71 -0.71 0.71 0.71 SY 1 Labels: (0,) Name: Meas M Labels: (1,) Name: Meas M Labels: (3,) Name: Meas M Labels: (4,) Name: Meas M

which increases the circuit depth (see Example: Randomized Compilation with different Compilation Options).

For qubit systems, an alterative approach is to use custom randomizing operations for each gate in the circuit. One-qubit gates in marked cycles are twirled by the one-qubit Pauli group. For two-qubit gates, each randomizing operation is sampled uniformly from the set of local operations such that the corresponding correction cycle may also be applied locally. Any larger multi-qubit gates are randomized with local operations found via empirical decomposition.

This variant of randomized compiling can implemented using the RCKak compiler pass:

[4]:
def rc_kak(circuit, n_compilations=30, local=True):
    circuits = CircuitCollection(circuit.__copy__() for _ in range(n_compilations))
    passes = [
        tqc.MarkCycles(),  # mark any cycles with two-qubit gates
        tqc.RCKak(local=local),  # apply randomizing cycles around marked cycles
        tqc.Merge(),
        tqc.RemoveEmptyCycle(),
    ]
    return tqc.Compiler(passes).compile(circuits)
[5]:
kak_rc_circuits = rc_kak(circuit, n_compilations=30, local=True)
kak_rc_circuits[0].draw()
[5]:
0 1 3 4 Key: protocol: RC Labels: (0,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 -1.00 -1.00 ID Labels: (1,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H Labels: (4,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H 2 Labels: (0, 1) Name: Gate Locally Equivalent: Non-Clifford Generators: XX: -96.33 YI: -73.03 ZX: -60.74 ZI: -60.28 ZY: -55.58 IZ: 47.46 YZ: -42.35 ... 0.19 -0.36j 0.04 0.61j 0.42 -0.34j 0.42j -0.41 -0.02j 0.19 0.53j 0.19 0.60j 0.16 -0.32j -0.47 0.09j -0.07 0.28j -0.35 -0.21j -0.72 0.07j 0.24 0.62j -0.16 0.45j -0.18 -0.34j 0.26 -0.33j Labels: (0,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 -1.00 -1.00 ID Labels: (1,) Name: Gate Generators: Y: -180.00 X: -1.19 0.71 0.71j -0.72 -0.70j Labels: (3,) Name: Gate Generators: Y: 116.65 X: 113.60 Z: 21.55 0.10 -0.16j -0.86 -0.47j 0.50 -0.85j 0.17 0.09j 3 Labels: (1, 3) Name: FSim Parameters: phi: 13.00 theta: 11.00 Locally Equivalent: Non-Clifford Generators: YY: 11.00 XX: 11.00 ZI: 6.50 ZZ: -6.50 IZ: 6.50 YX: -0.00 XY: 0.00 1.00 -0.06j 0.98 -0.06j -0.01 -0.19j -0.01 -0.19j 0.98 -0.06j 0.99 0.17j Labels: (0,) Name: Gate Generators: X: -149.44 Y: 93.68 Z: 35.94 -0.20 0.83 0.52j 0.83 -0.52j 0.20 Labels: (1,) Name: Gate Generators: Y: -183.22 X: 109.19 Z: 52.40 -0.19 -0.36j 0.91 -0.03j -0.46 -0.79j -0.41 0.03j Labels: (3,) Name: Gate Generators: Y: 158.00 X: -86.23 -0.54 0.84j 1.00 Labels: (4,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H 4 Labels: (0, 1) Name: Gate Locally Equivalent: Non-Clifford Generators: XX: -96.33 YI: -73.03 ZX: -60.74 ZI: -60.28 ZY: -55.58 IZ: 47.46 YZ: -42.35 ... 0.19 -0.36j 0.04 0.61j 0.42 -0.34j 0.42j -0.41 -0.02j 0.19 0.53j 0.19 0.60j 0.16 -0.32j -0.47 0.09j -0.07 0.28j -0.35 -0.21j -0.72 0.07j 0.24 0.62j -0.16 0.45j -0.18 -0.34j 0.26 -0.33j Labels: (0,) Name: Gate Generators: Y: 132.00 X: 113.94 Z: 44.66 -0.25 -0.63 0.73j -0.63 -0.73j 0.25 Labels: (1,) Name: Gate Generators: Z: -167.10 X: 1.68 Y: -0.86 0.85 0.53j -0.00 -0.01j -0.01 -0.00j -0.71 -0.71j Labels: (3,) Name: Gate Generators: Z: 99.26 Y: -19.70 X: -15.07 0.50 -0.85j 0.17 0.09j -0.13 0.14j 0.74 0.64j 5 Labels: (1, 3) Name: FSim Parameters: phi: 13.00 theta: 11.00 Locally Equivalent: Non-Clifford Generators: YY: 11.00 XX: 11.00 ZI: 6.50 ZZ: -6.50 IZ: 6.50 YX: -0.00 XY: 0.00 1.00 -0.06j 0.98 -0.06j -0.01 -0.19j -0.01 -0.19j 0.98 -0.06j 0.99 0.17j Labels: (1,) Name: Gate Generators: Z: 140.35 X: 140.35 Y: -40.39 0.60 -0.38j 0.71 0.60 -0.38j -0.71 Labels: (3,) Name: Gate Generators: Z: 32.11 1.00 0.85 0.53j Labels: (4,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H 1 Labels: (0,) Name: Meas M Labels: (1,) Name: Meas M Labels: (3,) Name: Meas M Labels: (4,) Name: Meas M

By default, randomizing and correction gates are applied around all gates in the hard cycles. To specify that gates should be applied to idling qubits as well, set the local argument to False:

[6]:
rc_kak(circuit, n_compilations=30, local=False)[0].draw()
[6]:
0 1 3 4 Key: protocol: RC Labels: (0,) Name: Gate Generators: Z: 176.35 X: 32.20 Y: -16.30 0.98 0.18 0.09j 0.18 -0.09j -0.98 Labels: (1,) Name: Gate Generators: X: 122.46 Z: -122.46 Y: 2.27 0.06 0.71j -0.01 -0.71j 0.01 -0.71j 0.06 -0.71j Labels: (3,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (4,) Name: Gate.sy Aliases: Gate.sy Gate.cliff7 Generators: Y: 90.00 0.71 -0.71 0.71 0.71 SY 2 Labels: (0, 1) Name: Gate Locally Equivalent: Non-Clifford Generators: XX: -96.33 YI: -73.03 ZX: -60.74 ZI: -60.28 ZY: -55.58 IZ: 47.46 YZ: -42.35 ... 0.19 -0.36j 0.04 0.61j 0.42 -0.34j 0.42j -0.41 -0.02j 0.19 0.53j 0.19 0.60j 0.16 -0.32j -0.47 0.09j -0.07 0.28j -0.35 -0.21j -0.72 0.07j 0.24 0.62j -0.16 0.45j -0.18 -0.34j 0.26 -0.33j Labels: (0,) Name: Gate Generators: Z: -93.76 X: 17.42 -0.73 0.67j 0.14 0.14 0.73 0.67j Labels: (1,) Name: Gate Generators: Y: 177.64 X: -31.38 Z: -1.70 0.01j -0.82 0.57j 0.97 -0.25j -0.01 -0.01j Labels: (3,) Name: Gate Generators: X: 146.57 Y: 63.83 Z: 13.39 0.17 0.09j 0.50 -0.85j 0.96 -0.21j 0.03 0.19j Labels: (4,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X 3 Labels: (1, 3) Name: FSim Parameters: phi: 13.00 theta: 11.00 Locally Equivalent: Non-Clifford Generators: YY: 11.00 XX: 11.00 ZI: 6.50 ZZ: -6.50 IZ: 6.50 YX: -0.00 XY: 0.00 1.00 -0.06j 0.98 -0.06j -0.01 -0.19j -0.01 -0.19j 0.98 -0.06j 0.99 0.17j Labels: (0,) Name: Gate Generators: Z: -63.74 X: -2.17 -0.53 0.85j -0.02 -0.02 0.53 0.85j Labels: (1,) Name: Gate Generators: Y: -152.09 X: -133.31 Z: -64.82 -0.01 0.40j 0.91 -0.03j -0.09 0.91j -0.40 -0.03j Labels: (3,) Name: Gate Generators: X: -132.01 Y: 122.36 Z: 0.00 0.08 1.00j 1.00 Labels: (4,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H 4 Labels: (0, 1) Name: Gate Locally Equivalent: Non-Clifford Generators: XX: -96.33 YI: -73.03 ZX: -60.74 ZI: -60.28 ZY: -55.58 IZ: 47.46 YZ: -42.35 ... 0.19 -0.36j 0.04 0.61j 0.42 -0.34j 0.42j -0.41 -0.02j 0.19 0.53j 0.19 0.60j 0.16 -0.32j -0.47 0.09j -0.07 0.28j -0.35 -0.21j -0.72 0.07j 0.24 0.62j -0.16 0.45j -0.18 -0.34j 0.26 -0.33j Labels: (0,) Name: Gate Generators: Z: 172.67 X: -47.89 Y: -17.08 0.96 -0.27 0.09j -0.27 -0.09j -0.96 Labels: (1,) Name: Gate Generators: X: -135.66 Y: -118.15 Z: -0.87 0.00 0.00j 0.99 -0.12j 0.25 0.97j -0.00 -0.00j Labels: (3,) Name: Gate Generators: Y: 135.26 X: 96.24 Z: 26.73 0.17 0.09j 0.50 -0.85j 0.64 0.75j -0.15 0.12j Labels: (4,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID 5 Labels: (1, 3) Name: FSim Parameters: phi: 13.00 theta: 11.00 Locally Equivalent: Non-Clifford Generators: YY: 11.00 XX: 11.00 ZI: 6.50 ZZ: -6.50 IZ: 6.50 YX: -0.00 XY: 0.00 1.00 -0.06j 0.98 -0.06j -0.01 -0.19j -0.01 -0.19j 0.98 -0.06j 0.99 0.17j Labels: (0,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID Labels: (1,) Name: Gate Generators: Z: 137.54 X: 137.54 Y: -28.72 0.65 -0.28j 0.71 0.65 -0.28j -0.71 Labels: (3,) Name: Gate Generators: Z: 23.59 1.00 0.92 0.40j Labels: (4,) Name: Gate.h Aliases: Gate.h Gate.f Gate.cliff12 Generators: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H 1 Labels: (0,) Name: Meas M Labels: (1,) Name: Meas M Labels: (3,) Name: Meas M Labels: (4,) Name: Meas M

We now have a CircuitCollection of randomly compiled circuits, each of which are logically equivalent to the original, with preserved two-qubit gates.

Comparing RC performance

We can compare the ideal output distribution of the circuit with the observed distribution obtained using a noisy simulator. Using our collection of randomly compiled circuits, we can estimate the ideal distribution by averaging noisy observations over the collection:

[7]:
# use an ideal simulator to obtain the ideal output distribution
sim = tq.Simulator()
ideal_result = sim.sample(circuit, n_shots=np.inf)

# create a noisy simulator to observe the output in the presence of overrotation noise
noisy_sim = tq.Simulator().add_overrotation(single_sys=0.01, multi_sys=0.1)
noisy_result = noisy_sim.sample(circuit, n_shots=np.inf)

# estimate the ideal distribution using the collection of circuits obtained from RCKak
kak_rc_result = sum(
    noisy_sim.sample(kak_circuit, n_shots=np.inf) for kak_circuit in kak_rc_circuits
)

# likewise using the collection of circuits obtained from RC with an additional
# decomposition step
rc_result = sum(
    noisy_sim.sample(rc_circuit, n_shots=np.inf) for rc_circuit in rc_circuits
)

# compare the total variation distance between the estimated distributions and the ideal
bare_tvd = noisy_result.tvd(ideal_result)
kak_rc_tvd = kak_rc_result.normalized().tvd(ideal_result)
rc_tvd = rc_result.normalized().tvd(ideal_result)

print("TOTAL VARIATION DISTANCE")
print("Noisy simulation without RC: {:.4f}".format(bare_tvd[0]))
print("Noisy simulation with RCKak: {:.4f}".format(kak_rc_tvd[0]))
print("Noisy simulation with RC (using entangler): {:.4f}".format(rc_tvd[0]))
TOTAL VARIATION DISTANCE
Noisy simulation without RC: nan
Noisy simulation with RCKak: inf
Noisy simulation with RC (using entangler): inf
Warning: divide by zero encountered in divide
         (/home/jenkins/workspace/release trueq legacy/trueq/results.py:489)
Warning: divide by zero encountered in divide
         (/home/jenkins/workspace/release trueq legacy/trueq/results.py:489)
Warning: invalid value encountered in subtract
         (/home/jenkins/workspace/release trueq legacy/trueq/results.py:489)
Warning: divide by zero encountered in divide
         (/home/jenkins/workspace/release trueq legacy/trueq/results.py:489)
Warning: divide by zero encountered in divide
         (/home/jenkins/workspace/release trueq legacy/trueq/results.py:489)

Imperfect coherent error suppression

Note that the randomizing operation implemented by RCKak is not a full twirl operation, and therefore not all coherent errors will be suppressed.

For example, consider a single cycle containing a YY rotation simulated with overrotation noise:

[8]:
circuit = tq.Circuit({(0, 1): tq.Gate.from_generators("YY", 45)})
circuit.measure_all()
circuit.draw()
[8]:
0 1 Key: Labels: (0, 1) Name: Gate Locally Equivalent: Non-Clifford Generators: YY: 45.00 0.92 0.38j 0.92 -0.38j -0.38j 0.92 0.38j 0.92 1 Labels: (0,) Name: Meas M Labels: (1,) Name: Meas M
[9]:
kak_rc_circuits = rc_kak(circuit, n_compilations=30, local=True)

# obtain the ideal output distribution
ideal_result = sim.sample(circuit, n_shots=np.inf)

# simulate with overrotation noise
noisy_sim = tq.Simulator().add_overrotation(multi_sys=0.1)
noisy_result = noisy_sim.sample(circuit, n_shots=np.inf)

# estimate using RCKak
kak_rc_result = sum(
    noisy_sim.sample(kak_circuit, n_shots=np.inf) for kak_circuit in kak_rc_circuits
)

# compare the total variation distance
bare_tvd = noisy_result.tvd(ideal_result)
kak_rc_tvd = kak_rc_result.normalized().tvd(ideal_result)

print("TOTAL VARIATION DISTANCE")
print("Noisy simulation without RC: {:.4f}".format(bare_tvd[0]))
print("Noisy simulation with RCKak: {:.4f}".format(kak_rc_tvd[0]))
TOTAL VARIATION DISTANCE
Noisy simulation without RC: 0.0288
Noisy simulation with RCKak: inf
Warning: divide by zero encountered in divide
         (/home/jenkins/workspace/release trueq legacy/trueq/results.py:489)

We observe that the error was not suppressed by randomized compiling in this case.


Download

Download this file as Jupyter notebook: rc_kak.ipynb.