Download

Download this file as Jupyter notebook: rc_kak.ipynb.

Example: Randomly compiling arbitrary two-qubit gates

This example shows how to use the RCKak compiler pass to randomly compile circuits with non-Clifford two-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: IX: 93.95 YX: 70.43 XI: -52.77 ZX: 52.65 IZ: -42.77 YY: 41.27 XY: 41.22 ... -0.54 0.21j 0.14 -0.57j -0.15 0.24j -0.46 0.16j 0.27 -0.48j 0.21 0.04j 0.23 0.04j -0.35 0.69j 0.04 0.21j 0.56 0.05j 0.42 0.60j 0.30 -0.07j 0.32 0.46j 0.53 -0.10j -0.38 -0.42j 0.11 0.24j Labels: (1,) Name: Gate.t Aliases: Gate.t Generators: Z: 45.00 1.00 0.71 0.71j T Labels: (3,) Name: Gate Generators: X: -80.98 Y: -56.98 Z: -39.65 0.51 0.43j 0.28 0.69j -0.56 0.50j 0.65 -0.16j 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: IX: 93.95 YX: 70.43 XI: -52.77 ZX: 52.65 IZ: -42.77 YY: 41.27 XY: 41.22 ... -0.54 0.21j 0.14 -0.57j -0.15 0.24j -0.46 0.16j 0.27 -0.48j 0.21 0.04j 0.23 0.04j -0.35 0.69j 0.04 0.21j 0.56 0.05j 0.42 0.60j 0.30 -0.07j 0.32 0.46j 0.53 -0.10j -0.38 -0.42j 0.11 0.24j Labels: (1,) Name: Gate.t Aliases: Gate.t Generators: Z: 45.00 1.00 0.71 0.71j T Labels: (3,) Name: Gate Generators: X: -80.98 Y: -56.98 Z: -39.65 0.51 0.43j 0.28 0.69j -0.56 0.50j 0.65 -0.16j 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: Y: 110.79 Z: 106.16 X: 64.70 0.54 -0.36j -0.18 -0.74j 0.74 0.20j -0.37 0.53j Labels: (1,) Name: Gate Generators: X: -73.89 Y: -31.19 Z: 6.99 0.59 0.49j -0.23 0.60j -0.59 0.25j 0.51 0.57j Labels: (3,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y 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: 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: -125.61 X: 125.61 Y: 3.61 -0.71 0.02j 0.71 -0.02j 0.71 0.02j 0.71 0.02j Labels: (1,) Name: Gate Generators: Y: -178.55 X: -22.84 0.99 0.13j -0.99 0.13j 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 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.cliff6 Aliases: Gate.cliff6 Generators: Y: -90.00 0.71j 0.71j -0.71j 0.71j 6 Labels: (1,) Name: Gate Generators: Z: 44.29 0.93 -0.38j 0.93 0.38j 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 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: X: -79.01 Z: 62.99 Y: -59.63 0.11 -0.68j 0.70 0.17j 0.03 0.72j 0.69 0.02j Labels: (1,) Name: Gate Generators: Z: -207.59 Y: 91.14 X: 49.16 -0.92 -0.01j 0.00 -0.40j 0.33 0.22j 0.51 -0.76j Labels: (3,) Name: Gate Generators: Y: 255.69 X: 83.73 Z: -53.18 -0.74 -0.03j -0.57 -0.34j 0.66 -0.06j -0.69 -0.29j 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.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: Z: -173.50 1.00 -0.06j -1.00 -0.06j Labels: (4,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID 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.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z Labels: (1,) Name: Gate.cliff13 Aliases: Gate.cliff13 Generators: X: -127.28 Z: 127.28 -0.71j 0.71j 0.71j 0.71j 13 Labels: (3,) Name: Gate Generators: Z: 169.00 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: 161.42 Y: 154.68 X: -28.66 -0.74 -0.20j -0.37 0.53j 0.54 -0.36j 0.18 0.74j Labels: (1,) Name: Gate Generators: Y: -244.42 X: -77.42 Z: 27.48 -0.47 0.42j 0.66 -0.41j -0.30 0.71j -0.35 0.53j Labels: (3,) Name: Gate Generators: Y: 74.53 Z: -74.53 X: -65.74 0.56 -0.43j 0.43 0.56j 0.50 -0.50j -0.50 -0.50j 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 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: Y: -89.97 Z: 2.58 X: 2.58 0.71 -0.02j 0.71 -0.02j -0.71 -0.02j 0.71 0.02j Labels: (1,) Name: Gate Generators: Z: -345.42 -0.99 0.13j -0.99 -0.13j Labels: (3,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (4,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z 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.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: (1,) Name: Gate Generators: X: -166.72 Y: -67.85 -0.93 0.38j -0.93 -0.38j 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: X: -159.08 Y: 140.26 Z: 132.79 -0.03 -0.72j -0.69 -0.02j -0.11 0.68j -0.70 -0.17j Labels: (1,) Name: Gate Generators: Y: 129.48 X: -70.55 Z: -56.85 0.33 0.22j -0.51 0.76j 0.92 0.01j 0.00 -0.40j Labels: (3,) Name: Gate Generators: Y: 95.99 Z: -27.21 X: -17.28 0.57 0.34j -0.74 -0.03j 0.69 0.29j 0.66 -0.06j Labels: (4,) Name: Gate.id Aliases: Gate.id Gate.i Gate.cliff0 Locally Equivalent: Identity Generators: I: 0.00 1.00 1.00 ID 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.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (1,) Name: Gate Generators: Y: 160.25 X: -132.10 Z: -132.10 -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.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X 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.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z Labels: (1,) Name: Gate.cliff13 Aliases: Gate.cliff13 Generators: X: -127.28 Z: 127.28 -0.71j 0.71j 0.71j 0.71j 13 Labels: (3,) Name: Gate Generators: X: 179.17 Y: 17.25 1.00 -0.10j 1.00 0.10j Labels: (4,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z 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.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: 89.87 Y: -5.63 X: 5.63 -0.75 -0.66j -0.05 -0.04j -0.04 0.05j 0.66 -0.75j Labels: (3,) Name: Gate Generators: X: -72.52 Y: 63.97 Z: -63.97 0.50 0.50j -0.50 0.50j 0.43 0.56j 0.56 -0.43j 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 two-qubit gate in the circuit. Each randomizing operation is sampled uniformly from the set of local operations such that the corresponding correction cycle may also be applied locally.

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 Generators: X: -121.44 Z: 115.85 Y: 65.04 -0.64 0.67 0.36j 0.67 -0.36j 0.64 Labels: (1,) Name: Gate Generators: Y: -141.86 Z: -22.75 X: 22.75 0.30 0.15j 0.93 -0.15j -0.93 -0.15j 0.30 -0.15j 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: IX: 93.95 YX: 70.43 XI: -52.77 ZX: 52.65 IZ: -42.77 YY: 41.27 XY: 41.22 ... -0.54 0.21j 0.14 -0.57j -0.15 0.24j -0.46 0.16j 0.27 -0.48j 0.21 0.04j 0.23 0.04j -0.35 0.69j 0.04 0.21j 0.56 0.05j 0.42 0.60j 0.30 -0.07j 0.32 0.46j 0.53 -0.10j -0.38 -0.42j 0.11 0.24j Labels: (0,) Name: Gate Generators: Y: 175.74 X: 36.46 Z: 13.67 0.08 0.20 -0.98j 0.20 0.98j -0.08 Labels: (1,) Name: Gate Generators: X: -167.38 Y: 66.39 Z: -0.55 0.00 0.00j 0.23 0.97j 0.83 0.55j -0.00 -0.00j Labels: (3,) Name: Gate Generators: X: -110.88 Z: -85.49 Y: -4.68 -0.17 0.64j -0.51 0.54j -0.56 0.50j 0.65 -0.16j 3 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: (0,) Name: Gate Generators: X: -121.44 Z: 115.85 Y: 65.04 -0.64 0.67 0.36j 0.67 -0.36j 0.64 Labels: (1,) Name: Gate Generators: Y: -129.49 X: -56.58 Z: 5.21 0.26 -0.21j 0.93 -0.15j -0.52 0.78j 0.30 -0.15j Labels: (3,) Name: Gate Generators: Z: 65.43 0.42 -0.91j 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: IX: 93.95 YX: 70.43 XI: -52.77 ZX: 52.65 IZ: -42.77 YY: 41.27 XY: 41.22 ... -0.54 0.21j 0.14 -0.57j -0.15 0.24j -0.46 0.16j 0.27 -0.48j 0.21 0.04j 0.23 0.04j -0.35 0.69j 0.04 0.21j 0.56 0.05j 0.42 0.60j 0.30 -0.07j 0.32 0.46j 0.53 -0.10j -0.38 -0.42j 0.11 0.24j Labels: (0,) Name: Gate Generators: Y: 175.74 X: 36.46 Z: 13.67 0.08 0.20 -0.98j 0.20 0.98j -0.08 Labels: (1,) Name: Gate Generators: Z: -15.09 Y: 0.34 X: 0.12 0.83 0.55j -0.00 -0.00j 0.00 0.95 0.32j Labels: (3,) Name: Gate Generators: X: 75.66 Z: -51.10 Y: 43.81 -0.56 0.50j 0.65 -0.16j 0.46 0.49j 0.19 0.72j 5 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 Generators: Y: -89.00 X: 15.75 Z: -15.75 0.66 0.24j 0.71 -0.66 -0.24j 0.71 Labels: (3,) Name: Gate Generators: X: -177.25 Y: -31.37 1.00 0.94 0.34j 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: -134.29 X: -118.52 Y: 17.90 0.75 0.66 0.10j 0.66 -0.10j -0.75 Labels: (1,) Name: Gate Generators: Y: 41.60 Z: -24.42 X: 24.42 0.89 0.21j -0.35 -0.21j 0.35 -0.21j 0.89 -0.21j 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.cliff6 Aliases: Gate.cliff6 Generators: Y: -90.00 0.71 0.71 -0.71 0.71 6 2 Labels: (0, 1) Name: Gate Locally Equivalent: Non-Clifford Generators: IX: 93.95 YX: 70.43 XI: -52.77 ZX: 52.65 IZ: -42.77 YY: 41.27 XY: 41.22 ... -0.54 0.21j 0.14 -0.57j -0.15 0.24j -0.46 0.16j 0.27 -0.48j 0.21 0.04j 0.23 0.04j -0.35 0.69j 0.04 0.21j 0.56 0.05j 0.42 0.60j 0.30 -0.07j 0.32 0.46j 0.53 -0.10j -0.38 -0.42j 0.11 0.24j Labels: (0,) Name: Gate Generators: Z: -156.79 X: -6.18 -0.98 0.20j -0.04 -0.04 0.98 0.20j Labels: (1,) Name: Gate Generators: Z: -98.91 Y: 98.29 X: -26.48 -0.41 0.61j -0.48 -0.48j 0.17 0.66j 0.73 -0.05j Labels: (3,) Name: Gate Generators: Y: -248.14 X: -120.43 Z: -13.14 -0.56 0.50j 0.65 -0.16j -0.28 0.61j -0.60 0.45j Labels: (4,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: 180.00 -1.00 1.00 Y 3 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: (0,) Name: Gate Generators: X: -126.40 Z: 111.55 0.66 0.10j -0.75 -0.75 -0.66 0.10j Labels: (1,) Name: Gate Generators: X: -113.94 Y: -68.74 Z: 15.79 0.19 -0.36j 0.89 0.21j 0.23 0.88j 0.35 -0.21j Labels: (3,) Name: Gate Generators: Z: -88.16 0.03 1.00j 1.00 Labels: (4,) Name: Gate.cliff13 Aliases: Gate.cliff13 Generators: X: 127.28 Z: -127.28 -0.71 0.71 0.71 0.71 13 4 Labels: (0, 1) Name: Gate Locally Equivalent: Non-Clifford Generators: IX: 93.95 YX: 70.43 XI: -52.77 ZX: 52.65 IZ: -42.77 YY: 41.27 XY: 41.22 ... -0.54 0.21j 0.14 -0.57j -0.15 0.24j -0.46 0.16j 0.27 -0.48j 0.21 0.04j 0.23 0.04j -0.35 0.69j 0.04 0.21j 0.56 0.05j 0.42 0.60j 0.30 -0.07j 0.32 0.46j 0.53 -0.10j -0.38 -0.42j 0.11 0.24j Labels: (0,) Name: Gate Generators: Y: -180.68 X: -36.93 -0.04 0.98 0.20j -0.98 0.20j -0.04 Labels: (1,) Name: Gate Generators: Z: -151.39 X: 139.04 Y: 114.71 -0.41 0.61j -0.48 -0.48j 0.38 -0.56j -0.52 -0.52j Labels: (3,) Name: Gate Generators: Y: 85.85 X: -43.54 Z: -2.17 0.65 -0.16j -0.56 0.50j 0.73 0.15j 0.64 -0.19j Labels: (4,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y 5 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: (0,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z Labels: (1,) Name: Gate Generators: Y: -85.38 X: -33.59 Z: 33.59 0.52 -0.48j 0.71 -0.52 0.48j 0.71 Labels: (3,) Name: Gate Generators: X: 167.50 Y: -65.90 1.00 0.73 -0.68j 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

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: 0.0746
Noisy simulation with RCKak: 0.0787
Noisy simulation with RC (using entangler): 0.0816

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: 0.0288

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


Download

Download this file as Jupyter notebook: rc_kak.ipynb.