I am looking the example of toric code in Pymatching.

Here is the code:

 import numpy as np
import matplotlib.pyplot as plt
from scipy.sparse import hstack, kron, eye, csr_matrix, block_diag

def repetition_code(n):
    row_ind, col_ind = zip(*((i, j) for i in range(n) for j in (i, (i+1)%n)))
    data = np.ones(2*n, dtype=np.uint8)
    return csr_matrix((data, (row_ind, col_ind)))

def toric_code_x_stabilisers(L):
    Hr = repetition_code(L)
    H = hstack(
            [kron(Hr, eye(Hr.shape[1])), kron(eye(Hr.shape[0]), Hr.T)],
    H.data = H.data % 2
    return csr_matrix(H)
def toric_code_x_logicals(L):
    H1 = csr_matrix(([1], ([0],[0])), shape=(1,L), dtype=np.uint8)
    H0 = csr_matrix(np.ones((1, L), dtype=np.uint8))
    x_logicals = block_diag([kron(H1, H0), kron(H0, H1)])
    x_logicals.data = x_logicals.data % 2
    return csr_matrix(x_logicals)
  1. I want to test minumum weight perfect matching in Pymatching. I want to run this code in the absence of noise and I want to see what will be the result of pymatching. To do that, I use the following :


    matching = pymatching.Matching(H)


Is that correct? I see nothing, is it because I have no error in the system or is it because there is some issue with matching.draw() function? Then I tried the following code scripts:

noise = np.array([0,0,0,0]) #no error
z = H@noise % 2
c = m.decode(z)

And I got the dimension mismatch error..

  1. I want to simulate surface code in the absence of error. It is simply I will initialize my data qubits in zero states and I will use x and z stabilizer. I will measure the stabilizers and I will see the result. For example, when I measure the X stabilizer, they will commute with Z stabilizer etc.. In that case, I am not sure what should change in toric code to make it surface code. It seemed to me that, toric_code_x_stabilisers can stay same. Do we have a small example for surface code?

1 Answer 1


Here is the working code example:

noise = np.array([0,0,0,0]) #no error
z = H@noise % 2
#c = m.decode(z)#since there is no error, no decoding too. I simply closed the line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.