OpenFermion
OpenFermion copied to clipboard
optimal_givens_decomposition has bad side effects
trafficstars
If you call optimal_givens_decomposition and then construct another circuit using the same qubits, the latter circuit can be incorrect. For example, below I create two circuits that should be identical, but the second one is different and incorrect:
import cirq
from openfermion import optimal_givens_decomposition
n_qubits = 3
qubits = cirq.LineQubit.range(n_qubits)
mat = cirq.testing.random_unitary(n_qubits)
circuit1 = cirq.Circuit(optimal_givens_decomposition(qubits, mat))
circuit2 = cirq.Circuit(optimal_givens_decomposition(qubits, mat))
circuit1
0: ──────────────PhISwap(0.25)──────────────────────────────────────────────PhISwap(0.25)──────────Z^-0.577───
│ │
1: ───Z^(-1/3)───PhISwap(0.25)^(-5/16)───PhISwap(0.25)───────────Z^0.351────PhISwap(0.25)^-0.435───Z^(1/7)────
│
2: ───Z^0.255────────────────────────────PhISwap(0.25)^(-8/15)───Z^-0.284─────────────────────────────────────
circuit2
0: ───PhISwap(0.25)───────────────────────PhISwap(0.25)─────Z^-0.577───
│ │
1: ───PhISwap(0.25)^0───PhISwap(0.25)─────PhISwap(0.25)^0───Z^(1/7)────
│
2: ─────────────────────PhISwap(0.25)^0───Z^-0.284─────────────────────
This is because optimal_givens_decomposition is mutating the variable mat I think. Can you confirm this?
You're right, the issue is that mat is being mutated.