abdullahkhalids / qecft

The book A Methods Focused Guide to Quantum Error Correction and Fault-Tolerant Quantum Computation
Other
17 stars 6 forks source link

Surface code implementation #5

Closed JiQing000 closed 1 year ago

JiQing000 commented 1 year ago

Solutions for issue#1 Implementation of surface codes. Please let me know whether there is any mistake or adjustment required.

abdullahkhalids commented 1 year ago

This is good starting work. Let me suggest some improvements.

In the current form, the code is not at all taking advantage of stac's abilities for constructing such circuits. Right now, most of the code is just figuring out the indices in a convoluted manner. The whole point of geometric addressing is to avoid doing this, and going mad.

Let us look at Fig 1. of the paper.

image

By drawing the two axes I have labelled each qubit by $(x, y)$. Now, from the diagram it is quite clear that for measurement qubit at $(x,y)$, the $CX$ gates will go between it and the qubits $(x+1,y), (x-1,y), (x,y+1), (x,y-1)$. So how do we do this in stac?

The key is when you create the physical qubits. Right now, you have

surface_code_circ.physical_register.elements = \
    [stac.PhysicalQubit(i, i, []) for i in range(surface_code_circ.num_qubits)]

In stac.PhysicalQubit(i, i, []) the second argument is the coordinate of the physical qubit. So, we want to turn this into $(x_i,y_j)$.

surface_code_circ.physical_register.elements = \
    [stac.PhysicalQubit(row*i + j, (i, j), []) for i in range(row) for j in range(col)]
# double check the first argument.

Next, in the following lines inside the for loop, you are assigning the physical qubits created above to the virtual qubits of the circuit.

surface_code_circ.register[(0,0,i)].constituent_register = surface_code_circ.physical_register.elements[i]
surface_code_circ.layout_map[(0,0,i)] = i

You want to rewrite the two loops, so you are alternatively assigning each physical qubit to either the data register or the syndrome register, as per the diagram.

Once you have done the above, the rest is trivial. Iterate through all the syndrome measurement qubits, look at its coordinates and do the $(x\pm 1, y\pm 1)$ business with the gates.

This should do the job. I hope the above is clear. If not, please ask.

abdullahkhalids commented 1 year ago

Lets also try to write relatively clean code from the start.

Thank you for your work.

JiQing000 commented 1 year ago

Thank you for the clarification! It is very kind of you to let me know all the improvements. I will work on them right away.

JiQing000 commented 1 year ago

@abdullahkhalids Hi, I have uploaded the revised solution into the files changed. Please let me know whether there is any other improvements and mistake. Thank you!

JiQing000 commented 1 year ago

@abdullahkhalids Hi. The revised 2nd version of the solution is ready for review. Pls let me know whether any other change is required. Thank you!

abdullahkhalids commented 1 year ago

I finally found the time to take a look at it. The logic seems sound as far as I have checked.

To finish this PR, kindly make the following minor changes:

You don't have to do the last thing. But if you do, when I write the Surface Code chapter of the book, I will be able to use this code, and identify you as a contributor to the book.

JiQing000 commented 1 year ago

Great! Thank you. I will make the changes accordingly, and I would be glad to let you use my code for your amazing book.

JiQing000 commented 1 year ago

@abdullahkhalids Thank you for the help and guidance. I have made changes according to your requests.