Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Shannon Decomposition error when synthesizing the GHZ unitary matrix #6725

Open
ACE07-Sev opened this issue Sep 12, 2024 · 4 comments
Open
Assignees
Labels
kind/bug-report Something doesn't seem to work. triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add

Comments

@ACE07-Sev
Copy link

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

@ACE07-Sev ACE07-Sev added the kind/bug-report Something doesn't seem to work. label Sep 12, 2024
@ACE07-Sev
Copy link
Author

If I print the unitary for the circuit, it's actually

array([[nan+nanj, nan+nanj, nan+nanj, nan+nanj],
       [nan+nanj, nan+nanj, nan+nanj, nan+nanj],
       [nan+nanj, nan+nanj, nan+nanj, nan+nanj],
       [nan+nanj, nan+nanj, nan+nanj, nan+nanj]])

Which explains the error.

@ACE07-Sev
Copy link
Author

ACE07-Sev commented Sep 13, 2024

@uzzzzzzz Can you kindly have a look please? I saw that you made the original commit for quantum_shannon_decomposition.py. I have tried a few random circuits, all fail, and give either the AssertionError as the result has nan values only, or they raise the ValueError for non-unitary matrix as .msb_demuxer() creates a non-unitary matrix and sends that to .quantum_shannon_decomposition().

You can try both cases for reproducibility:

  • AssertionError: GHZ state, or H(0) Y(1) state with two qubits.
  • ValueError: Uniform superposition state, basically Hadamard layer on all qubits.

I understand why the tests have passed before, as the code runs with unitarygroup.rvs(2**num_qubits).

@NoureldinYosri NoureldinYosri added the triage/discuss Needs decision / discussion, bring these up during Cirq Cynque label Sep 18, 2024
@ACE07-Sev
Copy link
Author

So, I implemented this natively on my side, and was able to get past the nan error, but still have the AssertionError problem. It's kind of confusing, it works for certain unitaries, and then for certain unitaries it doesn't.

I tried the following circuit, which is one of the cases that doesn't return the correct value (whilst for randomly generated unitaries, it works fine). A 4 qubit circuit with CX(0, 1) and CX(0, 2).

@NoureldinYosri
Copy link
Collaborator

related to #6666

@NoureldinYosri NoureldinYosri added triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add and removed triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Oct 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug-report Something doesn't seem to work. triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add
Projects
None yet
Development

No branches or pull requests

2 participants