本文整理汇总了Python中sympy.physics.quantum.qapply.qapply函数的典型用法代码示例。如果您正苦于以下问题:Python qapply函数的具体用法?Python qapply怎么用?Python qapply使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了qapply函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_RaisingOp
def test_RaisingOp():
assert Dagger(ad) == a
assert Commutator(ad, a).doit() == Integer(-1)
assert Commutator(ad, N).doit() == Integer(-1)*ad
assert qapply(ad*k) == (sqrt(k.n + 1)*SHOKet(k.n + 1)).expand()
assert qapply(ad*kz) == (sqrt(kz.n + 1)*SHOKet(kz.n + 1)).expand()
assert qapply(ad*kf) == (sqrt(kf.n + 1)*SHOKet(kf.n + 1)).expand()
assert ad.rewrite('xp').doit() == \
(Integer(1)/sqrt(Integer(2)*hbar*m*omega))*(Integer(-1)*I*Px + m*omega*X)
assert ad.hilbert_space == ComplexSpace(S.Infinity)
for i in range(ndim - 1):
assert ad_rep_sympy[i + 1,i] == sqrt(i + 1)
if not np:
skip("numpy not installed or Python too old.")
ad_rep_numpy = represent(ad, basis=N, ndim=4, format='numpy')
for i in range(ndim - 1):
assert ad_rep_numpy[i + 1,i] == float(sqrt(i + 1))
if not np:
skip("numpy not installed or Python too old.")
if not scipy:
skip("scipy not installed.")
else:
sparse = scipy.sparse
ad_rep_scipy = represent(ad, basis=N, ndim=4, format='scipy.sparse', spmatrix='lil')
for i in range(ndim - 1):
assert ad_rep_scipy[i + 1,i] == float(sqrt(i + 1))
assert ad_rep_numpy.dtype == 'float64'
assert ad_rep_scipy.dtype == 'float64'
示例2: test_CMod
def test_CMod():
assert qapply(CMod(4, 2, 2)*Qubit(0, 0, 1, 0, 0, 0, 0, 0)) == \
Qubit(0, 0, 1, 0, 0, 0, 0, 0)
assert qapply(CMod(5, 5, 7)*Qubit(0, 0, 1, 0, 0, 0, 0, 0, 0, 0)) == \
Qubit(0, 0, 1, 0, 0, 0, 0, 0, 1, 0)
assert qapply(CMod(3, 2, 3)*Qubit(0, 1, 0, 0, 0, 0)) == \
Qubit(0, 1, 0, 0, 0, 1)
示例3: test_WGate
def test_WGate():
nqubits = 2
basis_states = superposition_basis(nqubits)
assert qapply(WGate(nqubits)*basis_states) == basis_states
expected = ((2/sqrt(pow(2, nqubits)))*basis_states) - IntQubit(1, nqubits)
assert qapply(WGate(nqubits)*IntQubit(1, nqubits)) == expected
示例4: period_find
def period_find(a, N):
"""Finds the period of a in modulo N arithmetic
This is quantum part of Shor's algorithm.It takes two registers,
puts first in superposition of states with Hadamards so: ``|k>|0>``
with k being all possible choices. It then does a controlled mod and
a QFT to determine the order of a.
"""
epsilon = .5
#picks out t's such that maintains accuracy within epsilon
t = int(2*math.ceil(log(N, 2)))
# make the first half of register be 0's |000...000>
start = [0 for x in range(t)]
#Put second half into superposition of states so we have |1>x|0> + |2>x|0> + ... |k>x>|0> + ... + |2**n-1>x|0>
factor = 1/sqrt(2**t)
qubits = 0
for i in range(2**t):
qbitArray = arr(i, t) + start
qubits = qubits + Qubit(*qbitArray)
circuit = (factor*qubits).expand()
#Controlled second half of register so that we have:
# |1>x|a**1 %N> + |2>x|a**2 %N> + ... + |k>x|a**k %N >+ ... + |2**n-1=k>x|a**k % n>
circuit = CMod(t, a, N)*circuit
#will measure first half of register giving one of the a**k%N's
circuit = qapply(circuit)
print "controlled Mod'd"
for i in range(t):
circuit = measure_partial_oneshot(circuit, i)
# circuit = measure(i)*circuit
# circuit = qapply(circuit)
print "measured 1"
#Now apply Inverse Quantum Fourier Transform on the second half of the register
circuit = qapply(QFT(t, t*2).decompose()*circuit, floatingPoint=True)
print "QFT'd"
for i in range(t):
circuit = measure_partial_oneshot(circuit, i + t)
# circuit = measure(i+t)*circuit
# circuit = qapply(circuit)
print circuit
if isinstance(circuit, Qubit):
register = circuit
elif isinstance(circuit, Mul):
register = circuit.args[-1]
else:
register = circuit.args[-1].args[-1]
print register
n = 1
answer = 0
for i in range(len(register)/2):
answer += n*register[i + t]
n = n << 1
if answer == 0:
raise OrderFindingException(
"Order finder returned 0. Happens with chance %f" % epsilon)
#turn answer into r using continued fractions
g = getr(answer, 2**t, N)
print g
return g
示例5: test_LoweringOp
def test_LoweringOp():
assert Dagger(a) == ad
assert Commutator(a, ad).doit() == Integer(1)
assert Commutator(a, N).doit() == a
assert qapply(a*k) == (sqrt(k.n)*SHOKet(k.n-Integer(1))).expand()
assert qapply(a*kz) == Integer(0)
assert qapply(a*kf) == (sqrt(kf.n)*SHOKet(kf.n-Integer(1))).expand()
assert a().rewrite('xp').doit() == \
(Integer(1)/sqrt(Integer(2)*hbar*m*omega))*(I*Px + m*omega*X)
示例6: test_differential_operator
def test_differential_operator():
x = Symbol('x')
f = Function('f')
d = DifferentialOperator(Derivative(f(x), x), f(x))
g = Wavefunction(x**2, x)
assert qapply(d*g) == Wavefunction(2*x, x)
assert d.expr == Derivative(f(x), x)
assert d.function == f(x)
assert d.variables == (x,)
assert diff(d, x) == DifferentialOperator(Derivative(f(x), x, 2), f(x))
d = DifferentialOperator(Derivative(f(x), x, 2), f(x))
g = Wavefunction(x**3, x)
assert qapply(d*g) == Wavefunction(6*x, x)
assert d.expr == Derivative(f(x), x, 2)
assert d.function == f(x)
assert d.variables == (x,)
assert diff(d, x) == DifferentialOperator(Derivative(f(x), x, 3), f(x))
d = DifferentialOperator(1/x*Derivative(f(x), x), f(x))
assert d.expr == 1/x*Derivative(f(x), x)
assert d.function == f(x)
assert d.variables == (x,)
assert diff(d, x) == \
DifferentialOperator(Derivative(1/x*Derivative(f(x), x), x), f(x))
assert qapply(d*g) == Wavefunction(3*x, x)
# 2D cartesian Laplacian
y = Symbol('y')
d = DifferentialOperator(Derivative(f(x, y), x, 2) +
Derivative(f(x, y), y, 2), f(x, y))
w = Wavefunction(x**3*y**2 + y**3*x**2, x, y)
assert d.expr == Derivative(f(x, y), x, 2) + Derivative(f(x, y), y, 2)
assert d.function == f(x, y)
assert d.variables == (x, y)
assert diff(d, x) == \
DifferentialOperator(Derivative(d.expr, x), f(x, y))
assert diff(d, y) == \
DifferentialOperator(Derivative(d.expr, y), f(x, y))
assert qapply(d*w) == Wavefunction(2*x**3 + 6*x*y**2 + 6*x**2*y + 2*y**3,
x, y)
# 2D polar Laplacian (th = theta)
r, th = symbols('r th')
d = DifferentialOperator(1/r*Derivative(r*Derivative(f(r, th), r), r) +
1/(r**2)*Derivative(f(r, th), th, 2), f(r, th))
w = Wavefunction(r**2*sin(th), r, (th, 0, pi))
assert d.expr == \
1/r*Derivative(r*Derivative(f(r, th), r), r) + \
1/(r**2)*Derivative(f(r, th), th, 2)
assert d.function == f(r, th)
assert d.variables == (r, th)
assert diff(d, r) == \
DifferentialOperator(Derivative(d.expr, r), f(r, th))
assert diff(d, th) == \
DifferentialOperator(Derivative(d.expr, th), f(r, th))
assert qapply(d*w) == Wavefunction(3*sin(th), r, (th, 0, pi))
示例7: test_RaisingOp
def test_RaisingOp():
assert Dagger(ad) == a
assert Commutator(ad, a).doit() == Integer(-1)
assert Commutator(ad, N).doit() == Integer(-1)*ad
assert qapply(ad*k) == (sqrt(k.n + 1)*SHOKet(k.n + 1)).expand()
assert qapply(ad*kz) == (sqrt(kz.n + 1)*SHOKet(kz.n + 1)).expand()
assert qapply(ad*kf) == (sqrt(kf.n + 1)*SHOKet(kf.n + 1)).expand()
assert ad().rewrite('xp').doit() == \
(Integer(1)/sqrt(Integer(2)*hbar*m*omega))*(Integer(-1)*I*Px + m*omega*X)
assert ad.hilbert_space == ComplexSpace(S.Infinity)
示例8: test_quantum_fourier
def test_quantum_fourier():
assert QFT(0,3).decompose() == SwapGate(0,2)*HadamardGate(0)*CGate((0,), PhaseGate(1))\
*HadamardGate(1)*CGate((0,), TGate(2))*CGate((1,), PhaseGate(2))*HadamardGate(2)
assert IQFT(0,3).decompose() == HadamardGate(2)*CGate((1,), RkGate(2,-2))*CGate((0,),RkGate(2,-3))\
*HadamardGate(1)*CGate((0,), RkGate(1,-2))*HadamardGate(0)*SwapGate(0,2)
assert represent(QFT(0,3), nqubits=3)\
== Matrix([[exp(2*pi*I/8)**(i*j%8)/sqrt(8) for i in range(8)] for j in range(8)])
assert QFT(0,4).decompose() #non-trivial decomposition
assert qapply(QFT(0,3).decompose()*Qubit(0,0,0)).expand() ==\
qapply(HadamardGate(0)*HadamardGate(1)*HadamardGate(2)*Qubit(0,0,0)).expand()
示例9: test_cgate
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()
示例10: test_identity
def test_identity():
I = IdentityOperator()
O = Operator("O")
assert isinstance(I, IdentityOperator)
assert isinstance(I, Operator)
assert I.inv() == I
assert Dagger(I) == I
assert qapply(I * O) == O
assert qapply(O * I) == O
for n in [2, 3, 5]:
assert represent(IdentityOperator(n)) == eye(n)
示例11: test_UGate
def test_UGate():
a, b, c, d = symbols("a,b,c,d")
uMat = Matrix([[a, b], [c, d]])
# Test basic case where gate exists in 1-qubit space
u1 = UGate((0,), uMat)
assert represent(u1, nqubits=1) == uMat
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)))
示例12: _apply_op
def _apply_op(self, ket, orig_basis, **options):
state = ket.rewrite(self.basis)
# If the state has only one term
if isinstance(state, State):
return self._apply_operator(state, **options)
# state is a linear combination of states
return qapply(self*state).rewrite(orig_basis)
示例13: test_apply_represent_equality
def test_apply_represent_equality():
gates = [
HadamardGate(int(3 * random.random())),
XGate(int(3 * random.random())),
ZGate(int(3 * random.random())),
YGate(int(3 * random.random())),
ZGate(int(3 * random.random())),
PhaseGate(int(3 * random.random())),
]
circuit = Qubit(
int(random.random() * 2),
int(random.random() * 2),
int(random.random() * 2),
int(random.random() * 2),
int(random.random() * 2),
int(random.random() * 2),
)
for i in range(int(random.random() * 6)):
circuit = gates[int(random.random() * 6)] * circuit
mat = represent(circuit, nqubits=6)
states = qapply(circuit)
state_rep = matrix_to_qubit(mat)
states = states.expand()
state_rep = state_rep.expand()
assert state_rep == states
示例14: test_Hamiltonian
def test_Hamiltonian():
assert Commutator(H, N).doit() == Integer(0)
assert qapply(H*k) == ((hbar*omega*(k.n + Integer(1)/Integer(2)))*k).expand()
assert H().rewrite('a').doit() == hbar*omega*(ad*a + Integer(1)/Integer(2))
assert H().rewrite('xp').doit() == \
(Integer(1)/(Integer(2)*m))*(Px**2 + (m*omega*X)**2)
assert H().rewrite('N').doit() == hbar*omega*(N + Integer(1)/Integer(2))
示例15: test_NumberOp
def test_NumberOp():
assert Commutator(N, ad).doit() == ad
assert Commutator(N, a).doit() == Integer(-1)*a
assert Commutator(N, H).doit() == Integer(0)
assert qapply(N*k) == (k.n*k).expand()
assert N().rewrite('a').doit() == ad*a
assert N().rewrite('H').doit() == H/(hbar*omega) - Integer(1)/Integer(2)