# How to Switch Toric Code to Surface Code (no using STIM!)

Here is the toric code example which I found from :https://pymatching.readthedocs.io/en/latest/toric-code-example.html

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):
"""
Sparse check matrix for the X stabilisers of a toric code with
lattice size L, constructed as the hypergraph product of
two repetition codes.
"""
Hr = repetition_code(L)
H = hstack(
[kron(Hr, eye(Hr.shape)), kron(eye(Hr.shape), Hr.T)],
dtype=np.uint8
)
H.data = H.data % 2
H.eliminate_zeros()
return csr_matrix(H)



My problem is I want to switch this code to the surface code but I do not know how. I do not want to use STIM. In surface code, we have x and z stabilizers. I will add also a Z stabilizer function. This is fine but I do not know what will be different in this code to make it a surface code. It seemed to me X stabilizers can stay same. Probably I should change something(which can be boundaries) in the repetition code function, but I do not know how.

Can someone explain to me what I should change and how? Thanks

• I'm curious; what made you say that stim isn't allowed? Oct 26, 2022 at 6:39
• because you wrote a great library so when I asked this question, everyone answers me "use STIM" but I am not working with clifford circuits so I can't use STIM. Also I wonder how to switch this code without using STIM. Oct 26, 2022 at 7:09

The code sniplet in your question constructs the toric code as a hypergraph product (HGP) code using the non full-rank matrix of the repetition code : this is an $$n \times n$$ matrix with rank $$n-1$$. If you remove one of the rows (say the last one) then you have a full rank $$n-1 \times n$$ matrix. If you substitute this matrix for $$Hr$$ in the toric code routine, you'll get a surface code instead of toric code. This paper https://arxiv.org/abs/1202.0928 has the details : examples 5 and 6 on page 4.