Discrepancy between defined circuit and circuit.draw()

The gate-sequence defined in my circuit does not match with the sequence printed using circuit.draw().

     def circuit(params):
          qml.RZ(params[0,0], wires= 0)
          qml.RZ(params[0,1], wires= 1)
          qml.RZ(params[0,2], wires= 2)
          qml.RZ(params[0,3], wires= 3)
          return qml.probs(wires=[0,1,2,3])

     params= np.array([[0.1, 0.2, 0.5, 0.8], [1.2, 0.01, 2.4, 3.14]])

My circuit is printed like this:

The operations CZ[wires=[0,3]] and CZ(wires=[1,2]) got swapped in the circuit diagram. Is the diagram wrong, or the circuit being implemented is not as I defined in my qnode function?

However, I think here this won’t effect the final result because CZ operators acting on different pairs of qubits commute.

Hi @Vineesha!

As you note, the two operations CZ([1, 2]) and CZ([0, 3]) commute, and thus their order in the circuit diagram doesn’t affect the end result. The two circuits are equivalent.

The reason the circuit drawer may sometimes differ from the QNode is that, internally, we are constructing a directed acylic graph representing the circuit, and using this to determine the optimum circuit layout for visualization — avoiding odd gaps in the final diagram.

While this leads to more ‘compact’ visualizations, sometimes the operation order may be slightly altered!

1 Like

Thank you very much for your reply and detailed explanation, @josh!