TeamGraphix / graphix

measurement-based quantum computing (MBQC) compiler and simulator
https://graphix.readthedocs.io
Apache License 2.0
55 stars 20 forks source link

[Bug]: pattern optimization result in wrong output state in some cases #62

Closed shinich1 closed 1 year ago

shinich1 commented 1 year ago

Describe the bug

55 did not fully resolve the issue. some class of random Clifford+T circuits sometimes result in wrong output state.

To Reproduce run below multiple times:

from graphix import Circuit
import numpy as np
def genpair(n_qubits, count):
    pairs = []
    for i in range(count):
        choice = [j for j in range(n_qubits)]
        x = np.random.choice(choice)
        choice.pop(x)
        y = np.random.choice(choice)
        pairs.append((x, y))
    return pairs

def get_circuit():
    layers = 3
    n_qubits = 4
    circuit = Circuit(n_qubits)
    clifford_T_choice = [0,1,2,3,4,5,6]
    for i in range(layers):
        for j, k in genpair(n_qubits, 2):
            circuit.cnot(j, k)
        for j in range(n_qubits):
            k = np.random.choice(clifford_T_choice)
            if k == 0: # T gate
                circuit.rz(j, np.pi/4)
            elif k == 1: # T^daggar gate
                circuit.rz(j, -np.pi/4)
            elif k == 2: # H
                circuit.h(j)
            elif k == 3: # S
                circuit.s(j)
            elif k == 4: # X
                circuit.x(j)
            elif k == 5: # Z
                circuit.z(j)
            else:
                pass
    return circuit

circuit = get_circuit()
pattern = circuit.transpile()
pattern.standardize()
pattern.shift_signals()
pattern.perform_pauli_measurements()
pattern.minimize_space()
out_state = pattern.simulate_pattern()
state = circuit.simulate_statevector()
print("overlap of states: ", np.abs(np.dot(state.psi.flatten().conjugate(), out_state.psi.flatten())))

Expected behavior statevector simulation of original circuit and simulation of MBQC result in same state.

Environment (please complete the following information):

Additional context