Skip to content

CNOT(0,1).to_matrix() returns unexpected value #439

Description

@Lknadfodr

Describe the bug
CNOT(0,1).to_matrix() returns

$$ \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \\ 0 & 1 & 0 & 0 \end{pmatrix} $$

but I expect it to be

$$ \begin{pmatrix} 1 & 0 & 0 & 0 \\ 0 & 1 & 0 & 0 \\ 0 & 0 & 0 & 1 \\ 0 & 0 & 1 & 0 \end{pmatrix} $$

In comparison, SWAP(0,1), H(0)+H(1) are as expected, and when rewriting CZ(0,1) with H(1)+CX(0,1)+H(1) it also is as expected.

The behaviour of the cirquit itself however is correct, so when CNOT(0,1) is applied to |10>, one gets |11> instead of |10> as the matrix would imply.

Note also that CNOT(1,0).to_matrix() returns the expected matrix of CNOT(0,1), and vice versa, so the control and target seem to be swapped.

To Reproduce

import tequila as tq

U = tq.gates.CNOT(control=0, target=1)
print(U.to_matrix().real)

for b in [tq.QubitWaveFunction.from_string(b) for b in ["|00>", "|01>", "|10>", "|11>"]]:
    print(f"{b} -> {tq.simulate(objective=U, initial_state=b)}")

Expected behavior

Computer (please complete the following information):
tequila version: 1.9.10
python version: 3.10.12 (main, Nov 4 2025, 08:48:33) [GCC 11.4.0]
platform: #88~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Tue Oct 14 14:03:14 UTC 2

Additional context
I am trying to verify excercise 1 from Quantum Computing sheet 8, which resulted in a different matrix than I calculated by hands.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions