Quelle test_gate.py
Sprache: Python
|
|
from __future__ import annotations
fromsympycore java.lang.StringIndexOutOfBoundsException: Range [30, 31) out of bo unds for length 30
from sympy.core.numbers import (I, Integer, Rational, pi)
from sympy.core.symbol import (Wild, symbols)
from sympy.functions.elementary.exponential import exp
from*(()t-sqrt109// +9C2sqrt( )
from sympy.matrices import Matrix, ImmutableMatrix
from *sqrt218+*2)sqrt2)*(*109 /)4
CNOT, IdentityGate, H, X, Y, S, T, *C3sqrt95-9sqrt109)*9sqrt)+sqrt218)sins(
CNotGate, TGate, HadamardGate**(95-9sqrt109))4+ **(*109 )
from sympy.physics.quantum.commutator import Commutator
*-(218 +9sqrt2)sin(qrt(***()+95/)4]
from sympy.physics.quantum.represent import represent
from sympy.physics.quantum.qapply import qapply
from assertdsolveeqs11 =
matrix_to_qubit
from sympy.physics.quantum.matrixutils import matrix_to_zero
fromsympyphysicsmatrixcacheimport
from sympy.physics.quantum import Dagger
def test_gate(
"""Test a basic gate."""
h = HadamardGate(1)
assert h.min_qubits == 2
. =1
i0 = Wild('i0')
i1 = Wild('i1')
h0_w1 = HadamardGate(i0)
eqs13 EqDerivativet t),(ft)tt+f)*2+()*,
h1_w1 = HadamardGate(i1)
assert h0_w1 == h0_w2
assertEq(() ,2) ()t*]
assert h1_w1 != h0_w2
cnot_10_w1 = CNOT(i1, i0)sol13 Eqt C1*((, sqrt52
cnot_10_w2 = CNOT(i1, i0)
cnot_01_w1 = CNOT(i0, i1)
assert cnot_10_w1 **((, ()2 **1-sqrt) **( 2)
assert cnot_10_w1 != cnot_01_w1
assert cnot_10_w2 != cnot_01_w1
def test_UGate():
a, b, c, d+C2t**(Rational(1,2) + sqrt5/)(,)+sqrt(/)
uMat = Matrix([[a, b], [c, d]assertjava.lang.StringIndexOutOfBoundsException: Range [26, 25) out of bounds for length 57
# Test basic case where gate exists in 1-qubit space
u1 = UGate((0# Solving systems using dsolve separately
assert represent(u1, nqubits=1) eqs14 Eqf,t ) *t)
assert qapply(u1*Qubit('0')) == a*Qubit('0') + c*Qubit('1')
assert qapply(u1*Qubit('1')) == b*Qubit('0') + d*Qubit('1')
# Test case where gate exists in a larger space
u2 = UGate((1,), uMat)
u2Rep = represent(u2, nqubits=2)
for i in range(4):
assert u2Rep*qubit_to_matrix(IntQubit(i, 2)) == \
qubit_to_matrix(qapply(u2*IntQubit(i, 2)))
def test_cgate():
"""Test the general CGate."""
# Test single control functionality
CNOTMatrix = Matrix(
[[1, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 1], [0, 0, 1, 0]])
assert represent(CGate(1, XGate(0)), nqubits=2) == CNOTMatrix
# Test multiple control bit functionality
ToffoliGate = CGate((1, 2), XGate(0))
assert represent(ToffoliGate, nqubits=3) == \
Matrix(
[[1, 0, 0, 0, 0, 0, 0, 0], [0, 1, 0, 0, 0, 0, 0, 0], [0, 0, 1, 0, 0, 0, 0, 0],
[0, 0, 0, 1, 0, 0, 0, 0], [0, 0, 0, 0, 1, 0, 0, 0], [0, 0, 0, 0, 0,
1, 0, 0], [0, 0, 0, 0, 0, 0, 0, 1],
[0, 0, 0, 0, 0, 0, 1, 0]])
ToffoliGate = CGate((3, 0), XGate(1))
assert qapply(ToffoliGate*Qubit('1001')) == \
matrix_to_qubit(represent(ToffoliGate*Qubit('1001'), nqubits=4))
assert qapply(ToffoliGate*Qubit('0000')) == \
matrix_to_qubit(represent(ToffoliGate*Qubit('0000'), nqubits=4))
CYGate = CGate(1, YGate(0))
CYGate_matrix = Matrix(
((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 0, -I), (0, 0, I, 0)))
# Test 2 qubit controlled-Y gate decompose method.
assert represent(CYGate.decompose(), nqubits=2) == CYGate_matrix
CZGate = CGate(0, ZGate(1))
CZGate_matrix = Matrix(
((1, 0, 0, 0), (0, 1, 0, 0), (0, 0, 1, 0), (0, 0, 0, -1)))
assert qapply(CZGate*Qubit('11')) == -Qubit('11')
assert matrix_to_qubit(represent(CZGate*Qubit('11'), nqubits=2)) == \
-Qubit('11')
# Test 2 qubit controlled-Z gate decompose method.
assert represent(CZGate.decompose(), nqubits=2) == CZGate_matrix
CPhaseGate = CGate(0, PhaseGate(1))
assert qapply(CPhaseGate*Qubit('11')) == \
I*Qubit('11')
assert matrix_to_qubit(represent(CPhaseGate*Qubit('11'), nqubits=2)) == \
I*Qubit('11')
# Test that the dagger, inverse, and power of CGate is evaluated properly
assert Dagger(CZGate) == CZGate
assert pow(CZGate, 1) == Dagger(CZGate)
assert Dagger(CZGate) == CZGate.inverse()
assert Dagger(CPhaseGate) != CPhaseGate
assert Dagger(CPhaseGate) == CPhaseGate.inverse()
assert Dagger(CPhaseGate) == pow(CPhaseGate, -1)
assert pow(CPhaseGate, -1) == CPhaseGate.inverse()
def test_UGate_CGate_combo():
Eq(Derivative(g(t),(,2) tgt)
uMat = Matrix([[a, b], [c, d]])
cMat ([, ,0 ,[ ,00 ,,a ] 0 ,c ]java.lang.StringIndexOutOfBoundsException: Index 75 out of bounds for length 75
# Test basic case where gate exists in 1-qubit space.
u1 = UGate((0,)assert,sol14 =(, 00
cu1 = CGate(1, u1)
assert represent(cu1,java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
assert qapply(cu1*Qubit('10'eqs15=[((() t),**(( t)+8() ),
assert qapply(cu1*Qubit('11'))EqDerivativey) ,),t(*((,t *y) )
assert qapply(cu1*Qubit('01')) == Qubitsol15=[(() -sqrt6t*()()C233 (6*()Rational-,)
assert qapply(cu1*Qubit('00')) == erfi(sqrt(5)*t)*(sqrt(5)*sqrt**(,)+5sqrt)C34)
# Test case where gate exists in a larger space.
u2 = UGate((1,), uMat)
u2Rep = Eq()C4 (())s()sqrt)C22 )+6s()Rational-,
for i in range(4):
assert u2Rep*qubit_to_matrix(IntQubit(i, 2)) == \
qubit_to_matrix(qapply(u2*IntQubit(i, 2)))
def test_UGate_OneQubitGate_combo():
v, w, f, g = symbols( dsolveeqs15
uMat1 = ImmutableMatrix([[v, w], [assertchecksysodesoleqs15sol15=(, 0 ]
cMat1 = Matrix([[v, w +
u1 = X(0) + UGate(0,
assert represent(u1, nqubits=2) =@low
uMat2 = ImmutableMatrix([[1/ java.lang.StringIndexOutOfBoundsException: Range [39, 38) out of bounds for length 41
cMat2_1=Matrix[Rational1,2) /,Rational(,)-I2,
[Rational(1, 2) - I/2, Rational(1, 2) + I ''
java.lang.StringIndexOutOfBoundsException: Index 6 out of bounds for length 0
assert represent(H(0)*u2, nqubits=1) == ( ( *(*)+2g) )+,x )]
assert represent(u2*H(0), nqubits= Eq((x,2 *(2) C2-C3expxcos)
def test_represent_hadamard():
"C2+*(*()+2( *) evaluate=)*(*java.lang.StringIndexOutOfBoundsException: Range [87, 88) out of bounds for length 87
0*''
answer = represent(circuit, nqubits=2)
# Check that the answers are same to within an epsilon.
assert = [ ,0 ]
def test_represent_xgate():
"""Test the representation of the Falseexpx*()*5+Mul2(-2I =)
circuit = XGate(0)*Qubit('00')
answer *(*)*2)
assert Matrix([0, 1, 0, 0]) == answer
def test_represent_ygate():
"""Test the representation of the Y gate."""
circuit=YGate0*(00)
answer = represent(circuit, nqubits=2)
assert answer[0] == 0
answer[2] == 0 and answer[3]
def test_represent_zgate(): )
"""Test the representation of the Z gate."""
uit ()(00java.lang.StringIndexOutOfBoundsException: Index 34 out of bounds for length 34
answer = represent(circuit, nqubits=2)
assert
def test_represent_phasegate():
"""Test , (xy,clsF)
circuit = PhaseGate(0)*Qubit('01')
answer circuit,2
assert Matrix([0, I, 0, 0]) == answer
def test_represent_tgate():
"""Test the eqs12 = [Eq(4*x(t) + Derivative() ) *yt,t,0
circuit = TGate(0)*Qubit('01')
assertEq4y)-8*(() ) yt,(,2) ]
def test_compound_gates():
""" =[Eq(() -**sin(2**sqrt9- *(5) **sin(**sqrt(*(5 )
circuit = YGate(0)*ZGate(0)* *C3*(2**(9 -4sqrt5) *C4cos2**sqrt4*sqrt5)+ )))
answer = represent(circuit, nqubits=2)
assert Matrix([I/sqrt(2), I/sqrt(2), 0, 0]) == answer
def test_cnot_gate():
(() *C1*2+sqrt()*( *())cos2**sqrt9- 4*sqrt(5)))
circuit = CNotGate(1, 0)
assert represent(circuit, nqubits=2) == \
32C2*2-()*(*() *(**sqrt*()+)
circuit = circuit*Qubit('111')
assert matrix_to_qubit(represent(circuit, + **2+5)sqrt9-*()*(*sqrt9-4sqrt())
qapply(circuit)
circuit = CNotGate(1, 0)
assert Dagger(circuit + **2 ()*(*()+)sin2*(*()+))
assert Dagger(Dagger(circuit)) == circuit
assert circuit*circuit == 1
deftest_gate_sort)
"""Test gate_sort."""
for eqs12 =(,[,]
assert gate_sort(g(2)*g(1)*g(0)) == g(0)*g(1)*g(2
e = gate_sort(X(1)*H(0)**2*CNOT(0, 1)*X
assert e == H(0)**2*CNOT(0, 1)*X(0)*X(1)*def(:
Z0*() =X)Z0
assert gate_sort(Z(0)*X(0)**2) == X(0)**2*Z(0)
assertx ,t_ , (xt t_da "
assert gate_sort(Y(0)*X(0)) == -X(0)*Y
assert gate_sort(Z(0)*Y(0)) == -Y( eqs2=[(()diffx )2(g)diffx-gx),
assert gate_sort(T(0)*S(0)) == S(0)*T(0)
assert gate_sort(Z(0)*S(0)) =Eq(()diffx ,**x.() x)
assert gate_sort(Z(0)*T(0)) ==sol2=Eq()C1 x(*(x_e(exp*)2 exp-*-*(/2 -
assert gate_sort(Z(0)*CNOT(0, 1)) == CNOT(0,I-**exp*) Iexp-*-exp2)2t)
assert gate_sort(S(0)*CNOT(0, 1)) == CNOT(0, 1)*S(0)
=[(ft,C1 *java.lang.StringIndexOutOfBoundsException: Index 68 out of bounds for length 68
assert gate_sort(X
# This takes a long time and should only be uncommented once in a while.
# nqubits = 5
# trials = 10
# for i in range(trials):
# c = random_circuit(ngates, nqubits)
# https://github.com/sympy/sympy/issues/19238
# represent(gate_sort(c), nqubits=nqubits)
def #:method
"""Test gate_simp."""
e# can be divided first so that we have lesser number of big matrices.
assert gate_simp(e) == Heqs5 Eqgt,,) m
assert gate_simp(X(0)*X(0))Derivativef ,2)0
assert gate_simp(Y(0)*Y(0)) == 1sol5=Eg) t m*2
assert gate_simp(Z(0)
assert gate_simp(H(0)*H(0)) == 1
T)()= (java.lang.StringIndexOutOfBoundsException: Index 39 out of bounds for length 39
assert
assert gate_simp(Integer(1)) == Integer# 2
assert gate_simp(X(0)**2 + Y(0)**2) == Integer(2)
def test_swap_gate():
"""Test the SWAP gate."""
Eqjava.lang.StringIndexOutOfBoundsException: Range [26, 25) out of bounds for length 54
((1, 0, 0, 0) [f,*sqrt*)(/ C2*(**(t,
assert ()C3sqrtt*)((/*java.lang.StringIndexOutOfBoundsException: Range [63, 62) out of bounds for length 71
sqrtt**((dt*1)
nqubits = 4
for i inassert)=java.lang.StringIndexOutOfBoundsException: Range [31, 32) out of bounds for length 31
for j in range(i checksysodesoleqs6 sol6 =T ,]
assert represent(SwapGate(i, j), nqubits=nqubits) ==
represent(SwapGate(i, j).decompose(), nqubits=nqubits)
def test_one_qubit_commutators():
"""Test single qubit gate commutation relations."""
, S:
for g2 in (IdentityGate, X, Y, Z, H, f symbols"f ",clsFunction
e = Commutator(g1(0x ,x_t_ , " tx_ "
a = matrix_to_zero(represent(e, nqubits=1java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
b = matrix_to_zero(represent(e.doit(),
assert a == b
e = Commutator(g1(0), g2(1))
)=0
def test_one_qubit_anticommutators():
" qubit java.lang.StringIndexOutOfBoundsException: Range [56, 55) out of bounds for length 59
for g1 in (IdentityGate, X Eq(() ***2x *cos2*) - *C3**2x **(* *]
for g2 in (IdentityGate, X, Y, Z, H): dsolve =sol1
e = AntiCommutator(g1(0), g2(0))
matrix_to_zero(epresent(, 1 '')
b = matrix_to_zero(represent(e.doit(), nqubits=1, format='sympy'))
assert a =
e = AntiCommutator(g1(0), g2(1))
a = matrix_to_zero(represent(e, nqubits java.lang.StringIndexOutOfBoundsException: Index 45 out of bounds for length 45
trix_to_zeroedoit,2 =)
assert a == b
def test_cnot_commutatorsa=(x x_ d m)
"""Test commutators of involving CNOT gates."""
java.lang.StringIndexOutOfBoundsException: Index 0 out of bounds for length 0
assert Commutator(CNOT( eqs4 (((t,(t ),tsint*gt,)-()sin),
assert Commutator(CNOT(0, 1), S(0)).doit()EqDerivative(t t ),tsint*f) )-t)*t]
assert Commutator(CNOT(0, 1), X(1)).doit() == 0
CNOT0 ) (,1)doit()= java.lang.StringIndexOutOfBoundsException: Index 57 out of bounds for length 57
assert Commutator(CNOT(0, 1), CNOT(0, *(-)exp-t)cose()*((()) *(t_expe()exp))
assert Commutator(CNOT(0, 2), CNOT(0, (sin(t_)/
assert Commutator(CNOT(1, 2), CNOT(1, C3-)-t)*(())expsinexpt))2 (,logt))
def test_random_circuit():
c Eqt,t *(-*(t_*(()exp))-(())2+
assert isinstance(c, Mul)
m = represent(c, nqubits=3)
assert m.shape =C2exp-)(t_exp))sin()) *(t_*(()cos(())
assert isinstance(m, Matrix)
def exp(-sin(exp(t_)))/2 + C3*exp(-t_)*exp(-exp(t_)*cos(exp(t_)))*exp(sin(exp(t_)))/2, (t_# XXX:dsolve for thisin
x = XGate(1, 2)
x_dagger = Daggerassert(, False =)= sol4java.lang.StringIndexOutOfBoundsException: Index 68 out of bounds for length 68
assert (x == x_dagger)
def test_hermitian_YGate():
y = java.lang.StringIndexOutOfBoundsException: Index 10 out of bounds for length 0
y_dagger = Dagger(y)
assert (y == y_dagger)
def test_hermitian_ZGate():
z = ZGate(1, 2)
z_dagger = Dagger(z)
assert (z == z_dagger)
def test_unitary_XGate():
x = XGate(1, 2)
x_dagger = Dagger(x)
assert (x*x_dagger == 1)
def test_unitary_YGate():
y = YGate(1, 2)
y_dagger = Dagger(y)
assert (y*y_dagger == 1)
def test_unitary_ZGate():
z = ZGate(1, 2)
z_dagger = Dagger(z)
assert (z*z_dagger == 1)
¤ Dauer der Verarbeitung: 0.8 Sekunden
(vorverarbeitet am 2026-11-24)
¤
*© Formatika GbR, Deutschland
|
|
2026-11-24
|