Repository: quantumlib/Cirq

Issue #6666: Quantum shannon decomposition fails for QFT with non-unitary matrix error
Created: 2024-07-16T03:27:52Z
Description of the issue
I encountered an issue when attempting to decompose the unitary matrix of QFT(4) using the built-in quantum_shannon_decomposition function in Cirq.
According to the documentation, the quantum_shannon_decomposition function is supposed to decompose the unitary matrix into a sequence of CX, YPow, ZPow, and CNOT gates. However, it seems that the result of the decomposition contains a non-unitary matrix.
How to reproduce the issue
import cirq

qft4_qubits = cirq.NamedQubit.range(4, prefix="q")
qft4_op = cirq.qft(*qft4_qubits)
qft4_qc = cirq.Circuit(qft4_op)

qft4_utry = cirq.unitary(qft4_qc)

print(cirq.is_unitary(qft4_utry))   # True

qsd_qft4_qubits = cirq.NamedQubit.range(4, prefix="q")
qsd_qft4_op = cirq.quantum_shannon_decomposition(qsd_qft4_qubits, qft4_utry)
qsd_qft4_qc = cirq.Circuit(qsd_qft4_op)

qsd_qft4_utry = cirq.unitary(qsd_qft4_qc)

print(cirq.allclose_up_to_global_phase(qft4_utry, qsd_qft4_utry))


True

Traceback (most recent call last):
  File "/tmp/qsd_bug_cirq.py", line 11, in <module>
    qsd_qft4_qc = cirq.Circuit(qsd_qft4_op)
                  ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/anaconda3/envs/qcs/lib/python3.11/site-packages/cirq/circuits/circuit.py", line 1771, in __init__
    flattened_contents = tuple(ops.flatten_to_ops_or_moments(contents))
                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/anaconda3/envs/qcs/lib/python3.11/site-packages/cirq/ops/op_tree.py", line 134, in flatten_to_ops_or_moments
    yield from flatten_to_ops_or_moments(subtree)
  File "/anaconda3/envs/qcs/lib/python3.11/site-packages/cirq/ops/op_tree.py", line 133, in flatten_to_ops_or_moments
    for subtree in root:
  File "/anaconda3/envs/qcs/lib/python3.11/site-packages/cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py", line 89, in quantum_shannon_decomposition
    yield from _msb_demuxer(qubits, v1, v2)
  File "/anaconda3/envs/qcs/lib/python3.11/site-packages/cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py", line 158, in _msb_demuxer
    yield from quantum_shannon_decomposition(demux_qubits[1:], W)
  File "/anaconda3/envs/qcs/lib/python3.11/site-packages/cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py", line 66, in quantum_shannon_decomposition
    raise ValueError(
ValueError: Expected input matrix u to be unitary,                 but it fails cirq.is_unitary check


Cirq version
1.4.1

Duplicate issue #6725: Shannon Decomposition error when synthesizing the GHZ unitary matrix
Created: 2024-09-12T13:04:49Z
Description of the issue
Greetings there,
Hope all are well. I was trying the quantum_shannon_decomposition() function, and noticed that it doesn't work for the GHZ unitary matrix.
How to reproduce the issue
import cirq
from cirq.transformers.analytical_decompositions import quantum_shannon_decomposition
from scipy.stats import unitary_group

n_qubits = 2
ghz = cirq.Circuit()
qubits = [cirq.NamedQubit(f'q{i}') for i in range(n_qubits)]
ghz.append(cirq.H(qubits[0]))
ghz.append(cirq.CNOT(qubits[0], qubits[1]))

U = ghz.unitary()

qubits = [cirq.NamedQubit(f'q{i}') for i in range(n_qubits)]
circuit = cirq.Circuit(quantum_shannon_decomposition(qubits, U))

assert cirq.approx_eq(U, circuit.unitary(), atol=1e-9)

/home/ace07/Documents/GitHub/QICKIT/.venv/lib/python3.11/site-packages/cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py:121: RuntimeWarning: divide by zero encountered in scalar divide
  yield ops.ZPowGate(exponent=phi_2 / np.pi, global_shift=phase / phi_2).on(qubit)
/home/ace07/Documents/GitHub/QICKIT/.venv/lib/python3.11/site-packages/cirq/transformers/analytical_decompositions/quantum_shannon_decomposition.py:121: RuntimeWarning: invalid value encountered in scalar divide
  yield ops.ZPowGate(exponent=phi_2 / np.pi, global_shift=phase / phi_2).on(qubit)
/home/ace07/Documents/GitHub/QICKIT/.venv/lib/python3.11/site-packages/cirq/ops/common_gates.py:645: RuntimeWarning: invalid value encountered in scalar multiply
  p = 1j ** (2 * self._exponent * self._global_shift)
/home/ace07/Documents/GitHub/QICKIT/.venv/lib/python3.11/site-packages/cirq/ops/common_gates.py:645: RuntimeWarning: invalid value encountered in scalar multiply
  p = 1j ** (2 * self._exponent * self._global_shift)
  ---------------------------------------------------------------------------
AssertionError                            Traceback (most recent call last)
Cell In[7], [line 16](vscode-notebook-cell:?execution_count=7&line=16)
     [13](vscode-notebook-cell:?execution_count=7&line=13) qubits = [cirq.NamedQubit(f'q{i}') for i in range(n_qubits)]
     [14](vscode-notebook-cell:?execution_count=7&line=14) circuit = cirq.Circuit(quantum_shannon_decomposition(qubits, U))
---> [16](vscode-notebook-cell:?execution_count=7&line=16) assert cirq.approx_eq(U, circuit.unitary(), atol=1e-9)

AssertionError:


Cirq version
cirq == 1.4.1

Closing comment (2024-09-20T06:58:04Z): related to #6666