Download

Download this file as Jupyter notebook: rc_kak.ipynb.

Example: Randomly Compiling Arbitrary Multi-qudit Gates

This example shows how to use the RCKak compiler pass to randomly compile circuits with non-Clifford multi-qudit 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: IY: 98.18 XI: -88.55 XX: -76.77 IZ: 62.95 YX: -59.94 IX: 51.46 ZX: 42.38 ... -0.23 0.03j 0.07 -0.65j 0.16 0.65j 0.16 0.19j 0.31 -0.02j -0.27 0.54j -0.07 0.73j -0.00 -0.11j 0.24 0.44j 0.16 -0.25j 0.06 0.08j -0.67 -0.46j -0.68 0.36j -0.12 0.33j 0.04 0.07j -0.39 0.35j Labels: (1,) Name: Gate.t Aliases: Gate.t Generators: Z: 45.00 1.00 0.71 0.71j T Labels: (3,) Name: Gate Generators: Y: 117.17 X: 92.22 Z: -54.70 0.31 -0.23j -0.45 0.81j -0.68 -0.63j -0.36 -0.13j 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: IY: 98.18 XI: -88.55 XX: -76.77 IZ: 62.95 YX: -59.94 IX: 51.46 ZX: 42.38 ... -0.23 0.03j 0.07 -0.65j 0.16 0.65j 0.16 0.19j 0.31 -0.02j -0.27 0.54j -0.07 0.73j -0.00 -0.11j 0.24 0.44j 0.16 -0.25j 0.06 0.08j -0.67 -0.46j -0.68 0.36j -0.12 0.33j 0.04 0.07j -0.39 0.35j Labels: (1,) Name: Gate.t Aliases: Gate.t Generators: Z: 45.00 1.00 0.71 0.71j T Labels: (3,) Name: Gate Generators: Y: 117.17 X: 92.22 Z: -54.70 0.31 -0.23j -0.45 0.81j -0.68 -0.63j -0.36 -0.13j 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: Z: -211.58 X: 19.06 Y: 6.39 -0.22 0.97j -0.03 -0.08j 0.02 -0.09j -0.34 -0.94j Labels: (1,) Name: Gate Generators: Y: -169.85 Z: 21.60 X: -15.60 0.08 -0.12j 0.98 0.15j -0.99 0.03j 0.06 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.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.15 X: -125.15 Y: -4.56 0.71 -0.03j 0.71 -0.03j 0.71 0.03j -0.71 -0.03j Labels: (1,) Name: Gate Generators: Z: 162.31 -0.99 -0.15j 0.99 -0.15j 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.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: Y: -169.05 X: 61.82 0.94 -0.34j -0.94 -0.34j Labels: (3,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X 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: 174.32 X: -38.52 Y: -1.11 0.87 -0.45j -0.19 0.11j -0.19 0.10j -0.85 0.47j Labels: (1,) Name: Gate Generators: Z: -102.01 X: 28.87 Y: -2.99 0.53 0.82j 0.04 -0.21j -0.00 -0.22j 0.67 -0.71j Labels: (3,) Name: Gate Generators: Z: -97.96 Y: -45.46 X: -24.93 0.92 -0.03j 0.35 -0.16j -0.05 0.38j -0.25 -0.89j Labels: (4,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: -180.00 1.00 -1.00 Z 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.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (1,) Name: Gate Generators: X: -77.30 Z: -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.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X 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.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: (3,) Name: Gate Generators: Z: 169.00 1.00 0.10j -1.00 0.10j Labels: (4,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y 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: 32.59 Y: -10.02 X: -3.36 -0.34 -0.94j 0.02 -0.09j 0.03 0.08j 0.22 -0.97j Labels: (1,) Name: Gate Generators: Z: -155.82 Y: -119.59 X: -11.00 0.45 0.66j 0.47 -0.38j -0.39 0.46j -0.65 -0.46j Labels: (3,) Name: Gate Generators: Y: -130.19 Z: 130.19 X: 6.82 0.50 -0.50j 0.50 0.50j -0.45 -0.55j -0.55 0.45j Labels: (4,) Name: Gate.cliff13 Aliases: Gate.cliff13 Generators: X: 127.28 Z: -127.28 0.71j -0.71j -0.71j -0.71j 13 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.96 Z: 3.28 X: 3.28 0.71 -0.03j 0.71 -0.03j -0.71 -0.03j 0.71 0.03j Labels: (1,) Name: Gate Generators: Y: -177.86 X: -27.68 0.99 0.15j -0.99 0.15j Labels: (3,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (4,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X 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: Z: 127.28 X: 127.28 0.71 0.71 0.71 -0.71 H Labels: (1,) Name: Gate Generators: Z: -40.18 0.94 0.34j 0.94 -0.34j Labels: (3,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X Labels: (4,) Name: Gate.x Aliases: Gate.x Gate.cliff1 Generators: X: 180.00 1.00 1.00 X 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: Y: -24.92 Z: -1.48 X: 0.72 -0.87 0.45j -0.19 0.11j 0.19 -0.10j -0.85 0.47j Labels: (1,) Name: Gate Generators: Y: 121.93 X: -95.43 Z: -3.57 0.04 -0.21j 0.53 0.82j 0.67 -0.71j -0.00 -0.22j Labels: (3,) Name: Gate Generators: Z: 71.67 X: -42.65 Y: 23.38 0.92 -0.03j -0.35 0.16j -0.05 0.38j 0.25 0.89j 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.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: X: -77.30 Z: 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: -6.50 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.cliff6 Aliases: Gate.cliff6 Generators: Y: -90.00 0.71j 0.71j -0.71j 0.71j 6 Labels: (3,) Name: Gate Generators: X: -179.17 Y: -17.25 -1.00 0.10j -1.00 -0.10j Labels: (4,) Name: Gate.y Aliases: Gate.y Gate.cliff2 Generators: Y: -180.00 1.00 -1.00 Y 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: 72.01 X: -64.84 Y: 64.84 -0.04 -0.74j 0.04 0.67j 0.67 -0.04j 0.74 -0.04j Labels: (3,) Name: Gate Generators: X: 89.91 Y: 4.71 Z: 4.71 0.50 0.50j 0.50 -0.50j 0.55 -0.45j 0.45 0.55j Labels: (4,) Name: Gate.cliff6 Aliases: Gate.cliff6 Generators: Y: -90.00 0.71j 0.71j -0.71j 0.71j 6 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).

An alterative approach is to use custom randomizing operations for each gate in the circuit. One-qudit gates in marked cycles are twirled by the one-qudit 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 multi-qudit or 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 Generators: X: 149.08 Y: 95.49 Z: 32.53 0.18 0.83 -0.53j 0.83 0.53j -0.18 Labels: (1,) Name: Gate Generators: Y: -70.52 Z: 13.95 X: -13.95 0.80 -0.11j 0.57 0.11j -0.57 0.11j 0.80 0.11j 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: IY: 98.18 XI: -88.55 XX: -76.77 IZ: 62.95 YX: -59.94 IX: 51.46 ZX: 42.38 ... -0.23 0.03j 0.07 -0.65j 0.16 0.65j 0.16 0.19j 0.31 -0.02j -0.27 0.54j -0.07 0.73j -0.00 -0.11j 0.24 0.44j 0.16 -0.25j 0.06 0.08j -0.67 -0.46j -0.68 0.36j -0.12 0.33j 0.04 0.07j -0.39 0.35j Labels: (0,) Name: Gate Generators: X: -163.18 Z: 75.81 Y: 4.99 0.42 -0.91 -0.03j -0.91 0.03j -0.42 Labels: (1,) Name: Gate Generators: X: 147.55 Y: 108.66 Z: -55.87 0.04 0.31j -0.84 -0.44j 0.17 -0.94j -0.22 -0.22j Labels: (3,) Name: Gate Generators: X: -154.97 Z: 63.97 Y: -27.71 0.31 -0.23j -0.45 0.81j -0.70 0.60j -0.17 0.35j 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: 177.03 X: -26.71 Y: -18.62 0.98 -0.15 0.10j -0.15 -0.10j -0.98 Labels: (1,) Name: Gate Generators: X: -194.31 Y: -134.82 Z: -78.27 -0.25 0.57j 0.76 0.18j 0.09 0.78j -0.60 0.18j Labels: (3,) Name: Gate Generators: Z: 83.31 1.00 0.12 0.99j 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: IY: 98.18 XI: -88.55 XX: -76.77 IZ: 62.95 YX: -59.94 IX: 51.46 ZX: 42.38 ... -0.23 0.03j 0.07 -0.65j 0.16 0.65j 0.16 0.19j 0.31 -0.02j -0.27 0.54j -0.07 0.73j -0.00 -0.11j 0.24 0.44j 0.16 -0.25j 0.06 0.08j -0.67 -0.46j -0.68 0.36j -0.12 0.33j 0.04 0.07j -0.39 0.35j Labels: (0,) Name: Gate Generators: Y: 179.93 X: 4.02 Z: -3.18 -0.02 0.02 -1.00j 0.02 1.00j 0.02 Labels: (1,) Name: Gate Generators: X: -147.25 Y: 92.47 Z: -53.83 0.18 0.23j 0.16 0.94j 0.92 0.27j -0.21 -0.21j Labels: (3,) Name: Gate Generators: X: -151.11 Y: -73.36 Z: 70.03 0.31 -0.23j -0.45 0.81j -0.91 0.14j -0.33 0.21j 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: 144.44 X: 144.44 Y: -70.13 0.44 -0.56j 0.71 0.44 -0.56j -0.71 Labels: (3,) Name: Gate Generators: Z: 51.80 1.00 0.62 0.79j 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: Y: -151.45 X: 97.28 Z: -1.25 -0.01 0.54 0.84j 0.54 -0.84j 0.01 Labels: (1,) Name: Gate Generators: X: 123.33 Z: -123.33 Y: -55.74 -0.03 0.67j 0.30 -0.67j -0.30 -0.67j -0.03 -0.67j Labels: (3,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z Labels: (4,) Name: Gate.cliff13 Aliases: Gate.cliff13 Generators: Z: 127.28 X: -127.28 0.71 -0.71 -0.71 -0.71 13 2 Labels: (0, 1) Name: Gate Locally Equivalent: Non-Clifford Generators: IY: 98.18 XI: -88.55 XX: -76.77 IZ: 62.95 YX: -59.94 IX: 51.46 ZX: 42.38 ... -0.23 0.03j 0.07 -0.65j 0.16 0.65j 0.16 0.19j 0.31 -0.02j -0.27 0.54j -0.07 0.73j -0.00 -0.11j 0.24 0.44j 0.16 -0.25j 0.06 0.08j -0.67 -0.46j -0.68 0.36j -0.12 0.33j 0.04 0.07j -0.39 0.35j Labels: (0,) Name: Gate Generators: Y: 310.10 X: -4.85 -0.91 -0.42 0.01j 0.42 0.01j -0.91 Labels: (1,) Name: Gate Generators: X: 192.13 Y: 117.02 Z: -34.52 -0.36 0.23j -0.64 -0.64j 0.27 -0.86j -0.42 -0.04j Labels: (3,) Name: Gate Generators: Y: 40.95 X: -19.41 Z: 3.88 0.39 -0.84j -0.01 0.38j 0.31 -0.23j 0.45 -0.81j Labels: (4,) Name: Gate.z Aliases: Gate.z Gate.cliff3 Generators: Z: 180.00 1.00 -1.00 Z 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: -150.78 Y: -96.85 0.01 0.54 0.84j -0.54 0.84j 0.01 Labels: (1,) Name: Gate Generators: Y: 71.98 X: 44.90 Z: 8.65 -0.42 -0.61j -0.03 0.67j -0.59 -0.32j -0.30 -0.67j Labels: (3,) Name: Gate Generators: Z: -120.69 1.00 -0.51 -0.86j Labels: (4,) Name: Gate.cliff13 Aliases: Gate.cliff13 Generators: Z: 127.28 X: -127.28 0.71 -0.71 -0.71 -0.71 13 4 Labels: (0, 1) Name: Gate Locally Equivalent: Non-Clifford Generators: IY: 98.18 XI: -88.55 XX: -76.77 IZ: 62.95 YX: -59.94 IX: 51.46 ZX: 42.38 ... -0.23 0.03j 0.07 -0.65j 0.16 0.65j 0.16 0.19j 0.31 -0.02j -0.27 0.54j -0.07 0.73j -0.00 -0.11j 0.24 0.44j 0.16 -0.25j 0.06 0.08j -0.67 -0.46j -0.68 0.36j -0.12 0.33j 0.04 0.07j -0.39 0.35j Labels: (0,) Name: Gate Generators: X: -162.55 Z: 75.55 0.42 0.01j -0.91 -0.91 -0.42 0.01j Labels: (1,) Name: Gate Generators: Y: -127.70 X: -58.74 Z: 52.92 0.36 -0.23j 0.64 0.64j -0.90 0.07j 0.12 0.41j Labels: (3,) Name: Gate Generators: X: -173.03 Y: -125.73 Z: 45.56 -0.35 -0.16j 0.63 0.68j -0.45 0.81j -0.31 0.23j 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.y Aliases: Gate.y Gate.cliff2 Generators: Y: 180.00 -1.00 1.00 Y Labels: (1,) Name: Gate Generators: Y: 268.17 Z: 20.16 X: -20.16 -0.70 -0.11j -0.71 0.70 0.11j -0.71 Labels: (3,) Name: Gate Generators: X: 179.49 Y: 13.49 0.99 -0.15j 1.00 Labels: (4,) Name: Gate.cliff6 Aliases: Gate.cliff6 Generators: Y: -90.00 0.71 0.71 -0.71 0.71 6 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.0754
Noisy simulation with RCKak: 0.0402
Noisy simulation with RC (using entangler): 0.0766

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.

A Note on using RCKak for qudit gates and gates on more than two qubits

As mentioned above, RCKak inserts local randomizing and correction gates that depend on the gate to be randomized. For one-qudit and two-qubit gates, the group to sample from is calculated, and a randomization is always applied. For multi-qudit gates and larger multi-qubit gates, the group to sample from is not calculated, and instead randomization will be attempted numerically. If a solution is not found, randomizing and correction gates are not inserted.

However the RCKak pass can always be applied to cycles containing these gates without error:

[10]:
tq.settings.set_dim(3)

# Apply RCKak to a cycle containing two-qutrit gates
qutrit_rc_circuits = rc_kak(
    tq.Circuit({(0, 1): tq.Gate.random(9), (2, 3): tq.Gate.cx3}),
    n_compilations=30,
    local=True,
)
qutrit_rc_circuits[0].draw()
[10]:
0 1 2 3 Key: protocol: RC Labels: (2,) Name: Gate 0.99 0.12j 0.08 1.00j 0.67 -0.74j Labels: (3,) Name: Gate -0.18 0.31j -0.67 0.50j 0.41 0.09j -0.28 0.31j -0.18 -0.31j -0.10 -0.83j 0.77 0.33j -0.13 -0.39j 0.36 1 Labels: (0, 1) Name: Gate -0.16 0.36j -0.33 -0.21j -0.18 0.14j -0.07 -0.02j 0.09 -0.16j -0.06 -0.20j -0.28 0.12j -0.49 0.27j 0.04 -0.39j -0.08 0.25j 0.25 -0.45j -0.11 0.33j 0.07 -0.26j 0.04 0.15j -0.22 0.02j -0.09 0.40j 0.41 -0.22j -0.09 -0.04j 0.52 -0.22j 0.09 0.07j -0.42 -0.04j -0.30 -0.27j 0.12 -0.11j -0.14 -0.30j -0.09 -0.18j -0.00 -0.24j -0.23 -0.21j 0.09 -0.26j 0.00 -0.08j -0.08 0.43j 0.42 -0.12j 0.27 0.14j 0.36 -0.34j 0.30 0.05j -0.17 0.20j -0.10 0.15j -0.39 -0.12j -0.25 0.42j -0.35 0.07j 0.00 -0.02j 0.34 0.18j -0.24 0.18j 0.32 0.06j 0.13 -0.12j 0.07 -0.28j 0.03 -0.30j 0.06 -0.20j 0.36 -0.15j 0.38 0.26j 0.15 -0.15j -0.11 0.00j -0.02 -0.02j 0.07 -0.12j -0.24 -0.60j -0.14 0.10j 0.25 -0.27j -0.22 -0.18j -0.18 0.23j -0.26 -0.19j -0.11 -0.24j 0.68 0.01j -0.03 0.13j -0.04 -0.07j 0.05 -0.05j 0.33 0.01j 0.22 -0.07j -0.10 -0.35j -0.09 0.56j -0.22 0.21j 0.12 -0.06j -0.41 0.23j -0.08 -0.20j -0.15 -0.25j -0.15 -0.09j -0.02 0.22j -0.24 -0.27j -0.45 -0.06j 0.50 0.18j 0.03 -0.08j 0.23 0.07j 0.03 -0.39j Labels: (2, 3) Name: Gate.cx3 Aliases: Gate.cx3 Gate.cnot3 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 1.00 CX CX Labels: (2,) Name: Gate 0.92 0.39j 0.10 -1.00j 0.95 -0.32j Labels: (3,) Name: Gate -0.55 -0.63j -0.24 0.27j -0.21 0.36j 0.42 -0.27 0.79j 0.35 0.07j -0.11 -0.34j -0.21 -0.36j 0.82 -0.16j

Download

Download this file as Jupyter notebook: rc_kak.ipynb.