I'm running the following piece of code and get the following output.

n = 4
qc = QuantumCircuit(n)
for i in range(n - 1, -1, -1):
    print("h", i)
    for j in range(i - 1, -1, -1):
        qc.cp(math.pi / 2 ** (i - j), j, i)
        print("cp", j, i)

enter image description here I'm wondering why the second H gate appears before the third CP gate?

New contributor
Freeman Cheng is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.

1 Answer 1


The output circuit perfectly matches with the code.The second hadamard gate is not affecting the 3rd CP, so the order does not matter. Qiskit does this to make the output circuit image compact. A barrier can be put to match the sequence you expect from Qiskit.The same visual alteration is seen when applying measurements.Circuit with barrier


Your Answer

Freeman Cheng is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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