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

Bug: 2-qubit decomposition #1501

Open
Sam-XiaoyueLi opened this issue Oct 23, 2024 · 7 comments
Open

Bug: 2-qubit decomposition #1501

Sam-XiaoyueLi opened this issue Oct 23, 2024 · 7 comments
Assignees
Labels
bug Something isn't working transpiler
Milestone

Comments

@Sam-XiaoyueLi
Copy link
Contributor

The following code throws an error on Mac (apple silicon, regardless of qibo backend) but no error on i7:

from qibo.symbols import *
import qibo
from qibo.transpiler.unitary_decompositions import two_qubit_decomposition
L = 7
H_def = sum([ Z(x)*Z(x+1) +X(x)*X(x+1) +Y(x)*Y(x+1) +0.5*X(x) for x in range(L-1)])
H_sym = qibo.hamiltonians.SymbolicHamiltonian(H_def)
circ = H_sym.circuit(.1)
for gate in circ.queue:
    gate_decomposition = two_qubit_decomposition(
        *gate.qubits, gate.matrix()
    )
    for gate_elem in gate_decomposition:
        print(gate_elem)

Error message:

[Qibo 0.2.12|ERROR|2024-10-23 16:27:51]: Given state is not real in the magic basis.
ERROR:qibo.config:Given state is not real in the magic basis.
@Sam-XiaoyueLi Sam-XiaoyueLi added the bug Something isn't working label Oct 23, 2024
@renatomello
Copy link
Contributor

renatomello commented Oct 23, 2024

This is not exactly a bug. The documentation of two_qubit_decomposition says that the function implements Eq. (24) of arXiv:quant-ph/0307177. In the paper, one can see that this particular decomposition is only valid when the Hamiltonian that generates the unitary has no component in the $Z$ direction. Thus, this particular decomposition will fail for unitaries generated by Hamiltonians that do have components in the $Z$ direction.

I gues that the issue here is that the current decomposition is not general enough. The same reference provides in Eq. (6) a more general decomposition for unitaries that are in the $SU(4)$. People that are working more closely with the transpiler could take a look into it, e.g. @Simone-Bordoni or @csookim.

In [1]: from qibo import set_backend
   ...: from qibo.backends import NumpyBackend
   ...: from qibo.hamiltonians import SymbolicHamiltonian
   ...: from qibo.symbols import *
   ...: from qibo.transpiler.unitary_decompositions import two_qubit_decomposition
   ...: 
   ...: set_backend("numpy")
   ...: 
   ...: backend = NumpyBackend()
   ...: 
   ...: L = 7
   ...: 
   ...: H_def = sum([ Z(x)*Z(x+1) +X(x)*X(x+1) +Y(x)*Y(x+1) +0.5*X(x) for x in range(L-1)])
   ...: H_sym = SymbolicHamiltonian(H_def)
   ...: 
   ...: circ = H_sym.circuit(.1)
   ...: 
   ...: for k, gate in enumerate(circ.queue):
   ...:     try:
   ...:         gate_decomposition = two_qubit_decomposition(
   ...:             *gate.qubits, gate.matrix(backend), backend=backend
   ...:         )
   ...:         print(f"worked {k}")
   ...:     except:
   ...:         print(f"fail {k}")
   ...: 
[Qibo 0.2.13|INFO|2024-10-23 14:28:25]: Using numpy backend on /CPU:0
[Qibo 0.2.13|ERROR|2024-10-23 14:28:25]: Given state is not real in the magic basis.
fail 0
worked 1
worked 2
worked 3
worked 4
worked 5
worked 6
worked 7
worked 8
worked 9
worked 10
[Qibo 0.2.13|ERROR|2024-10-23 14:28:25]: Given state is not real in the magic basis.
fail 11

@marekgluza
Copy link
Contributor

Thanks @renatomello !

Are you saying that two_qubit_decomposition the code enters into a 2 CZ decomposition(24) and not

def cnot_decomposition(q0, q1, hx, hy, hz, backend):

which would be a 3 CZ decomposition?

What I mean is that two_qubit_decomposition should manage to enter here

else:
cnot_dec = cnot_decomposition(q0, q1, hx, hy, hz, backend=backend)
if ud is None:
return cnot_dec
gatelist = [
gates.Unitary(u1, q0),
gates.Unitary(backend.cast(H) @ v1, q1),
]
gatelist.extend(cnot_dec[1:])
g0, g1 = gatelist[-2:]
gatelist[-2] = gates.Unitary(u4 @ g0.parameters[0], q0)
gatelist[-1] = gates.Unitary(v4 @ g1.parameters[0], q1)

and decompose the 2 qubit unitary into 3 CZ.

It seems to fail in the magic basis decomposition though

if ud_diag is None:
u4, v4, ud, u1, v1 = magic_decomposition(unitary, backend=backend)
ud_diag = to_bell_diagonal(ud, backend=backend)
hx, hy, hz = calculate_h_vector(ud_diag, backend=backend)
hx, hy, hz = float(hx), float(hy), float(hz)
if np.allclose([hx, hy, hz], [0, 0, 0]):
u4, v4, ud, u1, v1 = magic_decomposition(unitary, backend=backend)
gatelist = [gates.Unitary(u4 @ u1, q0), gates.Unitary(v4 @ v1, q1)]

The two_qubit_decomposition function should manage to decompose into 3 CZs any 2 qubit unitary. If it throws an error for XXZ which is already in the canonical form then it seems it is a bug because it throws an error while the 3 CZ decomposition can be provided 'by hand' and exists

@marekgluza
Copy link
Contributor

marekgluza commented Oct 23, 2024

Added notes:
Set $A = X\otimes X + Y\otimes Y +Z\otimes Z$ then $A+0.5 Z$ has a 3 CZ decomposition which runs in the above codes. Then I can take that decomposition and conjugate by Hadamards $(H\otimes H)(A+0.5Z)(H\otimes H) = A +0.5 X$ so just by redifining the outer single-qubit unitaries I get the decomposition.
To me this means that we should decide between:

  • fixing something failing in the current decomposition based on the magic basis
    or
  • dust off the proposal to implement the KAK KAK decomposition #1384 (comment)
    and provide something stable

@renatomello
Copy link
Contributor

@marekgluza @Sam-XiaoyueLi The most immediate solution is to extend the current decomposition to when the magic-basis expansion has non-zero imaginary part, i.e. extend from Eq. (24) of arXiv:quant-ph/0307177 to Eq. (6) of the same paper. Maybe @csookim could help us with that?

@csookim
Copy link
Member

csookim commented Oct 24, 2024

Ok. I will take a look.

@csookim
Copy link
Member

csookim commented Oct 25, 2024

@renatomello I am not familiar with the paper arXiv:quant-ph/0307177 used in the code. Instead, I will review arXiv:quant-ph/0308006 and look for alternatives. @Sam-XiaoyueLi, is this urgent?

@marekgluza
Copy link
Contributor

marekgluza commented Oct 25, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working transpiler
Projects
None yet
Development

No branches or pull requests

5 participants