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

Expressions are parsed incorrectly by Defgate #1734

Closed
bramathon opened this issue Feb 6, 2024 · 1 comment · Fixed by #1756
Closed

Expressions are parsed incorrectly by Defgate #1734

bramathon opened this issue Feb 6, 2024 · 1 comment · Fixed by #1756
Labels
bug 🐛 An issue that needs fixing. urgent Priority assistance requested

Comments

@bramathon
Copy link
Collaborator

Code Snippet

import numpy as np
from pyquil.quilbase import DefGate
from pyquil.quilatom import quil_exp, Parameter

phi = Parameter("phi")

rz = np.array(
    [
        [quil_exp(-1j*phi/2), 0],
        [0, quil_exp(+1j*phi/2)],
    ]
)


print(DefGate(name="blarg", matrix=rz, parameters=[phi]))

rz2 = np.array(
    [
        [quil_exp(-1j*phi/2)*np.exp(1j*np.pi/4), 0],
        [0, quil_exp(+1j*phi/2)*np.exp(1j*np.pi/4)],
    ]
)

print(rz2[0,0])
print(DefGate(name="blarg", matrix=rz2, parameters=[phi]))

Error Output

The first defgate outputs as expected.

The second output is the expression, EXP(-i*%phi/2)*0.7071067811865476+0.7071067811865475i
While the expression is ambiguous, a closer look reveals that it is structured correctly:
Mul(Function('EXP',Div(Mul((-0-1j),Parameter('phi')),2),<ufunc 'exp'>),(0.7071067811865476+0.7071067811865475j))
The exponential is multiplied by the complex number.

In the defgate, it's clear this is not the case. The expression is the exponential multiplied by the real part of the complex number, while the imaginary part is then added after the fact. Ie:

(a+bi)*exp(theta)

vs

a*exp(theta) + bi

A closer look at the matrix reveals this to be the case:

Add(Mul(Function('EXP',Div(Mul((-0-1j),Parameter('phi')),(2+0j)),<ufunc 'exp'>),(0.7071067811865476+0j)),0.7071067811865475j)

DEFGATE blarg(%phi) AS MATRIX:
	exp((-1.0i*%phi)/2), 0
	0, exp((1.0i*%phi)/2)

EXP(-i*%phi/2)*0.7071067811865476+0.7071067811865475i
DEFGATE blarg(%phi) AS MATRIX:
	(exp((-1.0i*%phi)/2)*0.7071067811865476)+0.7071067811865475i, 0
	0, (exp((1.0i*%phi)/2)*0.7071067811865476)+0.7071067811865475i
@bramathon bramathon added the bug 🐛 An issue that needs fixing. label Feb 6, 2024
@bramathon
Copy link
Collaborator Author

Possibly related to #1682

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An issue that needs fixing. urgent Priority assistance requested
Projects
None yet
2 participants