We learned that in the 5-Qibit error correcting code, the logical zero state $ | \bar{0} \rangle $ is encoded in the form of $ \frac{1}{4} (\mathbb{1}+M_0)(\mathbb{1}+M_1)(\mathbb{1}+M_2)(\mathbb{1}+M_3) | 00000 \rangle $ where $M_0 = Z_1X_2X_3Z_4, \,\, $ $M_1 = Z_2X_3X_4Z_0, \,\,$ $M_2 = Z_3X_4X_0Z_1, \,\,$ $M_3 = Z_4 X_0 X_1 Z_2.$

I am trying to represent the codeword operator shown above in Qiskit in the form of a quantum circuit but having a hard time getting started.

What is the best way to represent an operator which is a sum of the identity operator + some composition (tensor product of quantum operators)

p.s. (I guess an intermediate step is to figure out how to construct an operator like $(\mathbb{1}+M_0)$ and apply it to quantum circuit in "from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister" )

p.s. (this was suggested to me as a potential source of information that answers my question. Although it was helpful to improve my knowledge, given that the post was mainly about the Steane code (7 qubit error correcting code), I wasn't able to figure out the answer to my question.)

  • $\begingroup$ @AbdullahKhalid thank you so much for suggesting a relevant post. Although it did help me gain knowledge, i still have the same question. I suppose I can literally write out $2^5$ x $2^5$ matrices to represent the $ \frac{1}{4} (\mathbb{1}+M_0)(\mathbb{1}+M_1)(\mathbb{1}+M_2)(\mathbb{1}+M_3)$ but I was hoping I could implement the operator in the framework of Qiskit rather than having to literally write it out. Simply put, is there a way to write out matrix multiplication and tensor product of Pauli matrixes in the Qiskit framework? None of the examples I have Googled answers this.. $\endgroup$
    – Blackwidow
    May 25, 2023 at 0:48
  • $\begingroup$ Are you interested in constructing the circuit or the matrix? $\endgroup$ May 25, 2023 at 0:51
  • $\begingroup$ @AbdullahKhalid Yes. For example, I would love to be able to apply $ \mathbb{1} + Z_4 X_0 X_1 Z_2 $ using something like "from qiskit.opflow import I, X, Z, PauliSumOp" or some package within Qiskit. All examples I have found so far are just applying single qubit gate to each qubit one by one. When I have to do that for an operator like $ | \bar{0} \rangle $ is encoded in the form of $ \frac{1}{4} (\mathbb{1}+M_0)(\mathbb{1}+M_1)(\mathbb{1}+M_2)(\mathbb{1}+M_3) | 00000 \rangle $, it gets super clunky $\endgroup$
    – Blackwidow
    May 25, 2023 at 0:55

1 Answer 1


You don't have to chain together mini circuits of $I+M_i$ to create the final $|\bar 0\rangle$ state. There is a much simpler way (with fewer gates) that is given in the linked answer. You can also see a more detailed explanation here.


import stac
cd = stac.CommonCodes.generate_code('[[5,1,3]]')

Encoding circuit for 5,1,3 code

You can easily rewrite this circuit in qiskit. In fact, here is the QASM for it

include "qelib1.inc";

qreg q[5];
creg c[5];

h q[0];
cz q[0],q[1];
cz q[0],q[3];
cx q[0],q[4];
cz q[0],q[4];
h q[1];
cz q[1],q[2];
cz q[1],q[3];
cx q[1],q[4];
h q[2];
cz q[2],q[0];
cz q[2],q[1];
cx q[2],q[4];
h q[3];
cz q[3],q[0];
cz q[3],q[2];
cx q[3],q[4];
cz q[3],q[4];

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