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}$)...

  • $\begingroup$ 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... $\endgroup$
    – draks ...
    Nov 1, 2020 at 21:36

3 Answers 3


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: IBM Q quantum circuit and the quantum circuit from the reference is circuit from reference (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):
    if ipt==1:
    # controlled phase flip - if the input state is |1>,
    # then flip the global phase by pi
    # g1


    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]:
    return 0

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

def checkPhases():
    for i in range(32):
        print('expectation value for state '+bin(i)[2:]+' and the physical qubits of 0:')
        print('expectation value for state '+bin(i)[2:]+' and the physical qubits of 1:')

Then call the function--


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).

  • $\begingroup$ 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... $\endgroup$
    – draks ...
    Nov 3, 2020 at 12:16
  • 1
    $\begingroup$ 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. $\endgroup$ Nov 5, 2020 at 8:13
  • $\begingroup$ cool, so we found a flaw in the paper? $\endgroup$
    – draks ...
    Nov 5, 2020 at 9:48
  • $\begingroup$ 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 $\endgroup$ Nov 5, 2020 at 12:47
  • $\begingroup$ 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? $\endgroup$
    – draks ...
    Nov 6, 2020 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: enter image description here

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: enter image description here (the $|\psi\rangle$ in this picture actually refers to what I've called $|\phi\rangle$ in this answer.

  • $\begingroup$ $\newcommand{\bra}[1]{\left<#1\right|}\newcommand{\ket}[1]{\left|#1\right>}\newcommand{\bk}[2]{\left<#1\middle|#2\right>}\newcommand{\bke}[3]{\left<#1\middle|#2\middle|#3\right>}$ +1 thanks, can we use the fact that a $X_1$ on the input $\ket\phi$ has to act like a $XXXXX$ on the logical space? I mean a unitary taking us to the logical code space should fulfill that, right? $\endgroup$
    – draks ...
    Oct 20, 2020 at 13:52
  • $\begingroup$ doesn't that point towards a sequence of CNOTs ...? $\endgroup$
    – draks ...
    Oct 20, 2020 at 14:33
  • $\begingroup$ I mean during the implementation of all the other controlled operations... $\endgroup$
    – draks ...
    Oct 20, 2020 at 15:00
  • $\begingroup$ yes, that's right. $\endgroup$
    – DaftWullie
    Oct 20, 2020 at 15:07
  • $\begingroup$ wanna check my quirk update above? $\endgroup$
    – draks ...
    Oct 20, 2020 at 15:55

I think this is another valid solution.

enter image description here

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.

  • $\begingroup$ +1 looks very nice, but are sure about all the phases? I agree that this is cumbersome to check... $\endgroup$
    – draks ...
    Nov 4, 2020 at 12:52
  • $\begingroup$ Pretty sure. Have you found any inconsistencies? $\endgroup$
    – tsgeorgios
    Nov 4, 2020 at 13:15
  • $\begingroup$ No all good, thanks...How did you get to that solution? $\endgroup$
    – draks ...
    Nov 5, 2020 at 9:47
  • $\begingroup$ +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... $\endgroup$
    – draks ...
    Nov 6, 2020 at 8:50
  • $\begingroup$ 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) $ $\endgroup$
    – tsgeorgios
    Nov 6, 2020 at 8:56

Your Answer

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

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