2
$\begingroup$

I'm trying to use sympy to compute symbolic results of quantum circuits using dirac notation. More specifically, I'd like to use the three rotation gates. I try to implement them in the following way, using sympy symbolic quantum physics package :

from IPython.display import display

from sympy import init_printing
init_printing(use_latex=True)
from sympy import expand, Eq, Symbol, simplify, exp, sin,cos,I,srepr,Matrix
from sympy.physics.quantum import *
from sympy.physics.quantum.qubit import *
from sympy.physics.quantum.gate import *
from sympy.physics.quantum.circuitplot import circuit_plot


theta = Symbol('theta')

Ry_mat = Matrix([[cos(theta/2), -sin(theta/2)],[sin(theta/2),cos(theta/2)]]) #check
Rx_mat = Matrix([[cos(theta/2), -I*sin(theta/2)],[-I*sin(theta/2),cos(theta/2)]]) #check

Ry = UGate((0,), Ry_mat)
Rx= UGate((1,), Rx_mat)
c = Ry*Rx
circuit_plot(c, nqubits=2);

qapply(c*Qubit('00'))

I get the error unhashable type: 'MutableDenseMatrix' when executing qapply. I tried to replace the line :

c = Rx*Ry

with

c = Rx

and then there is no more errors. Does someone know what's going on ? What am I doing wrong ?

$\endgroup$
1
$\begingroup$

I don't know why this doesn't work with Matrix, but a fix for this would be to use ImmutableMatrix instead

from sympy import Symbol, sin, cos, I, ImmutableMatrix
from sympy.physics.quantum import qapply
from sympy.physics.quantum.qubit import Qubit
from sympy.physics.quantum.gate import UGate

theta = Symbol('theta')

Ry_mat = ImmutableMatrix([[cos(theta/2), -sin(theta/2)],
                        [sin(theta/2),cos(theta/2)]])
Rx_mat = ImmutableMatrix([[cos(theta/2), -I*sin(theta/2)],
                        [-I*sin(theta/2) ,cos(theta/2)]])

Ry = UGate((0,), Ry_mat)
Rx= UGate((1,), Rx_mat)
c = Ry* Ry

print(qapply(c*Qubit('00'))) # -sin(theta/2)**2*|00> + 2*sin(theta/2)*cos(theta/2)*|01> + cos(theta/2)**2*|00>

I've filed an issue for your problem. In the meantime, use ImmutableMatrix.

| improve this answer | |
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.