# Nielsen&Chuang 5-qubit quantum error-correction encoding gate


In Nielsen&Chuang on quantum error-correction codes they give a 5 qubit one as follows: $$\small \ket{0_L}=\frac14\left( \color{red}{ +\ket{00000} -\ket{00011} +\ket{00101} -\ket{00110} } % \color{blue}{ +\ket{01001} +\ket{01010} -\ket{01100} -\ket{01111}}\\ \hspace{.47in} % \color{green}{ -\ket{10001} +\ket{10010} +\ket{10100} -\ket{10111}} % -\ket{11000} -\ket{11011} -\ket{11101} -\ket{11110} \right)$$

$$\small \ket{1_L}=\frac14\left( \color{red}{ +\ket{11111} -\ket{11100} +\ket{11010} -\ket{11001} } % \color{blue}{ +\ket{10110} +\ket{10101} -\ket{10011} -\ket{10000}}\\ \hspace{.47in} % \color{green}{ -\ket{01000} +\ket{01011} +\ket{01101} -\ket{01110} } % -\ket{00001} -\ket{00010} -\ket{00100} -\ket{00111} \right)$$ (I reshuffled entries w.r.t. to equal bits of information...colors represents equal patterns of signs, which remind me on a $$H\otimes H$$ gate)

How to implement the encoding gate for this?

I can't find that specific one anywhere in the online available literature...

Update: I can take $$\ket{00000}$$ to $$\ket0_L$$ by that quirk (modulus some sign errors for the other transfer to $$\ket{1_L}$$)...

• I'm looking for a gate based implementation without ancillas and measurements. In case of more than one answer, the simplest one gets the bounty... Nov 1 '20 at 21:36

To access and edit the quantum circuit and view the Bloch sphere of the quantum state online, click the hyperlink(to run and see the Bloch sphere you have to sign in). The final code is also posted on the git-hub(some self-written script of the project is called in that file). The circuit is as follows: and the quantum circuit from the reference is (Chandak, S., Mardia, J., & Tolunay, M. Implementation and analysis of stabilizer codes in pyQuil.).

After I completed the construction follows the upper reference, a global phase factor pi shows up for the logical state of |1>, so I appended a controlled-phase flip operation(CU1(pi), CX, CU1(pi), CX). To get the logical state of |0>, just remove the leftmost X gate.

Here comes another reference: Gottesman, Daniel. "Stabilizer codes and quantum error correction." arXiv preprint quant-ph/9705052 (1997).

For the detail of the stabilize code encoding, you can read section 4.2(Network for Encoding) of the Gottesman book or section 10.5.8(Quantum circuits for encoding, decoding, and correction) of the Nielson book.

Here comes my code, first I generated the physical qubits

from qiskit import QuantumRegister,ClassicalRegister,QuantumCircuit,Aer,execute
from qiskit.providers.aer import QasmSimulator
from qiskit.circuit.library.standard_gates import CU1Gate
from numpy import pi
def physicalQubits(ipt):
qr=QuantumRegister(5)
circ=QuantumCircuit(qr)
if ipt==1:
circ.x(qr[0])
# controlled phase flip - if the input state is |1>,
# then flip the global phase by pi
CU1=CU1Gate(pi)
circ.append(CU1,[qr[0],qr[1]])
circ.cx(qr[0],qr[1])
circ.append(CU1,[qr[0],qr[1]])
circ.cx(qr[0],qr[1])

circ.h(qr[4])
circ.s(qr[4])
# g1
circ.cz(qr[4],qr[3])
circ.cz(qr[4],qr[1])
circ.cy(qr[4],qr[0])

circ.h(qr[3])
#g2
circ.cz(qr[3],qr[2])
circ.cz(qr[3],qr[1])
circ.cx(qr[3],qr[0])

circ.h(qr[2])
#g3
circ.cz(qr[2],qr[4])
circ.cz(qr[2],qr[3])
circ.cx(qr[2],qr[0])

circ.h(qr[1])
circ.s(qr[1])
#g4
circ.cz(qr[1],qr[4])
circ.cz(qr[1],qr[2])
circ.cy(qr[1],qr[0])
return circ.to_gate()


Then, you can get(in this place, to get the following output, you have to append the classical register and the corresponding measurement and execute instruction by yourself, since the requirement of this question is to not use classical register)

But until this place, I only showed you that the quantum states are correct, while the phase continues unknown, so what follows is the code to check the phase:

from qiskit.aqua.operators import StateFn,I
def ini(circ,qr,ipt):
# Input binary form, and append [0] ahead for qr1 block.
for i in range(len(ipt)):
if ipt[len(ipt)-i-1]:
circ.x(qr[i])
return 0

def Dec2Bi(num):
# Decimal to binary list.
res=list(bin(num)[2:])
return [int(res[i]) for i in range(len(res))]

def checkPhases():
operator=I.tensorpower(5)
for i in range(32):
qr=QuantumRegister(5)
circ=QuantumCircuit(qr)
ini(circ,qr,Dec2Bi(i))
psi=StateFn(circ)
phi1=StateFn(physicalQubits(0))
print('expectation value for state '+bin(i)[2:]+' and the physical qubits of 0:')
print((~psi@operator@phi1).eval())
phi2=StateFn(physicalQubits(1))
print('expectation value for state '+bin(i)[2:]+' and the physical qubits of 1:')
print((~psi@operator@phi2).eval())


Then call the function--

checkPhases()


You'll get the result and if the phase is non-zero, the result of the corresponding one should be a negative one.

Another way to check that this construction of gate works correctly is to get the state vector(psi0) of the constructed state and the state vector(psi1) after an arbitrary stabilizer is acted on it and see whether <psi0|psi1> equals one. I have checked the result but the code is omitted(you can still access the corresponding on git-hub).

• Wow +1 so far. I tried to code your second circuit and found some deviations to mine resp. your results. See my quirk. Can you help here? maybe it is just a swap or some nots... Nov 3 '20 at 12:16
• I have seen your problem. The lowermost qubit is the input, right? The rightmost operation of that qubit should be controlled0Pauli Y, not controlled-Pauli Z.See this link. Nov 5 '20 at 8:13
• cool, so we found a flaw in the paper? Nov 5 '20 at 9:48
• Maybe, this quantum circuit has an additional global phase $pi$, but only the relative phase is physically detectable, so sometimes this kind of deviances are just ignored. Also, I only the basics about quantum error correction, so maybe an investigation is your next object? LOL Nov 5 '20 at 12:47
• maybe... Would you agree that we need 6 local and 12 controlled operations to implement your second circuit compared to 5 locals and 9 controlled in tsgeorgios answer? Nov 6 '20 at 7:49

While there must be unitary ways of doing this, one possible way is to measure the stabilizers. What I mean by this is, say you have a single qubit state $$|\phi\rangle=\alpha|0\rangle+\beta|1\rangle$$. You take 4 ancilla qubits, $$|\psi\rangle=|\phi\rangle|0000\rangle,$$ and you measure the 4 stabilizers $$\{K_i\}$$ of the code. You're aiming to get the system into the +1 eigenspace of all 4 operators. (So if you get the wrong measurement results, you have to perform an error correction.)

The circuit that you need for each stabilizer looks like:

As a slightly more robust preparation procedure, you might simply prepare logical 0 of the code (again by stabilizer measurements) and teleport your unknown state into the system. Here's a suitable circuit for that last step: (the $$|\psi\rangle$$ in this picture actually refers to what I've called $$|\phi\rangle$$ in this answer.

• doesn't that point towards a sequence of CNOTs ...? Oct 20 '20 at 14:33
• I mean during the implementation of all the other controlled operations... Oct 20 '20 at 15:00
• yes, that's right. Oct 20 '20 at 15:07
• wanna check my quirk update above? Oct 20 '20 at 15:55

I think this is another valid solution.

Let me try to explain the thought process:

$$| 0_L \rangle$$ $$\big( | 1_L \rangle \big)$$ is a superposition of all states with an even (odd) number of 1s. From $$| 0 \rangle^{\otimes 5}$$ state, we map the first 4 - qubits in a uniform superposition and we fix the parity with $$CNOT$$ gates. This will give even 1s if we begin from $$| 0 \rangle^{\otimes 4} |0 \rangle$$ and odd 1s from $$| 0 \rangle^{\otimes 4} |1 \rangle$$. Now we should just fix the phases. We observe that a state in $$| 0_L \rangle$$ has -1 iff the number of pairs of 'adjacent' (in a ring topology) qubits both in $$|1 \rangle$$ state is odd. But we can compute this with a collection of $$CZ$$ gates. The only caveat is that $$| 0 \rangle^{\otimes 4} |1 \rangle$$ will be mapped to $$- |1_L \rangle$$. But we can easily fix this with a $$Z$$ gate acting on the last qubit right in the beginning.

• +1 looks very nice, but are sure about all the phases? I agree that this is cumbersome to check... Nov 4 '20 at 12:52
• Pretty sure. Have you found any inconsistencies? Nov 4 '20 at 13:15
• No all good, thanks...How did you get to that solution? Nov 5 '20 at 9:47
• +50 thanks, Yitian agreed that your solution is the most efficient. I accepted his answer because of the references, which I asked for in the main question. And thanks a lot for your explanation. I need to think about the ring topology... Nov 6 '20 at 8:50
• Thanks. Regarding ring topology, I simply mean that when you count the pairs that are in $|11 \rangle$ state consider $(q_0, q_1)$, $(q_1, q_2)$, $(q_2, q_3)$, $(q_3, q_4)$ and $(q_4, q_0)$ Nov 6 '20 at 8:56