def test_j2(): j, m = symbols('j m') assert Commutator(J2, Jz).doit() == 0 assert qapply(J2 * JzKet(1, 1)) == 2 * hbar**2 * JzKet(1, 1) assert qapply(J2 * JzKet( j, m)) == j**2 * hbar**2 * JzKet(j, m) + j * hbar**2 * JzKet(j, m) assert J2.matrix_element(1, 1, 1, 1) == 2 * hbar**2
def test_fermion_states(): c = FermionOp("c") # Fock states assert (FermionFockBra(0) * FermionFockKet(1)).doit() == 0 assert (FermionFockBra(1) * FermionFockKet(1)).doit() == 1 assert qapply(c * FermionFockKet(1)) == FermionFockKet(0) assert qapply(c * FermionFockKet(0)) == 0 assert qapply(Dagger(c) * FermionFockKet(0)) == FermionFockKet(1) assert qapply(Dagger(c) * FermionFockKet(1)) == 0
def main(): print() print('Demonstration of Grover\'s Algorithm') print('The OracleGate or V Gate carries the unknown function f(x)') print( '> V|x> = ((-1)^f(x))|x> where f(x) = 1 when x = a (True in our case)') print('> and 0 (False in our case) otherwise') print() nqubits = 2 print('nqubits = ', nqubits) v = OracleGate(nqubits, black_box) print('Oracle or v = OracleGate(%r, black_box)' % nqubits) print() psi = superposition_basis(nqubits) print('psi:') pprint(psi) demo_vgate_app(v) print('qapply(v*psi)') pprint(qapply(v * psi)) print() w = WGate(nqubits) print('WGate or w = WGate(%r)' % nqubits) print('On a 2 Qubit system like psi, 1 iteration is enough to yield |1>') print('qapply(w*v*psi)') pprint(qapply(w * v * psi)) print() nqubits = 3 print('On a 3 Qubit system, it requires 2 iterations to achieve') print('|1> with high enough probability') psi = superposition_basis(nqubits) print('psi:') pprint(psi) v = OracleGate(nqubits, black_box) print('Oracle or v = OracleGate(%r, black_box)' % nqubits) print() print('iter1 = grover.grover_iteration(psi, v)') iter1 = qapply(grover_iteration(psi, v)) pprint(iter1) print() print('iter2 = grover.grover_iteration(iter1, v)') iter2 = qapply(grover_iteration(iter1, v)) pprint(iter2) print()
def main(): print() print("Demonstration of Grover's Algorithm") print("The OracleGate or V Gate carries the unknown function f(x)") print( "> V|x> = ((-1)^f(x))|x> where f(x) = 1 when x = a (True in our case)") print("> and 0 (False in our case) otherwise") print() nqubits = 2 print("nqubits = ", nqubits) v = OracleGate(nqubits, black_box) print("Oracle or v = OracleGate(%r, black_box)" % nqubits) print() psi = superposition_basis(nqubits) print("psi:") pprint(psi) demo_vgate_app(v) print("qapply(v*psi)") pprint(qapply(v * psi)) print() w = WGate(nqubits) print("WGate or w = WGate(%r)" % nqubits) print("On a 2 Qubit system like psi, 1 iteration is enough to yield |1>") print("qapply(w*v*psi)") pprint(qapply(w * v * psi)) print() nqubits = 3 print("On a 3 Qubit system, it requires 2 iterations to achieve") print("|1> with high enough probability") psi = superposition_basis(nqubits) print("psi:") pprint(psi) v = OracleGate(nqubits, black_box) print("Oracle or v = OracleGate(%r, black_box)" % nqubits) print() print("iter1 = grover.grover_iteration(psi, v)") iter1 = qapply(grover_iteration(psi, v)) pprint(iter1) print() print("iter2 = grover.grover_iteration(iter1, v)") iter2 = qapply(grover_iteration(iter1, v)) pprint(iter2) print()
def main(): print print 'Demonstration of Grover\'s Algorithm' print 'The OracleGate or V Gate carries the unknown function f(x)' print '> V|x> = ((-1)^f(x))|x> where f(x) = 1 when x = a (True in our case)' print '> and 0 (False in our case) otherwise' print nqubits = 2 print 'nqubits = ', nqubits v = OracleGate(nqubits, black_box) print 'Oracle or v = OracleGate(%r, black_box)' % nqubits print psi = superposition_basis(nqubits) print 'psi:' pprint(psi) demo_vgate_app(v) print 'qapply(v*psi)' pprint(qapply(v*psi)) print w = WGate(nqubits) print 'WGate or w = WGate(%r)' % nqubits print 'On a 2 Qubit system like psi, 1 iteration is enough to yield |1>' print 'qapply(w*v*psi)' pprint(qapply(w*v*psi)) print nqubits = 3 print 'On a 3 Qubit system, it requires 2 iterations to achieve' print '|1> with high enough probability' psi = superposition_basis(nqubits) print 'psi:' pprint(psi) v = OracleGate(nqubits, black_box) print 'Oracle or v = OracleGate(%r, black_box)' % nqubits print print 'iter1 = grover.grover_iteration(psi, v)' iter1 = qapply(grover_iteration(psi, v)) pprint(iter1) print print 'iter2 = grover.grover_iteration(iter1, v)' iter2 = qapply(grover_iteration(iter1, v)) pprint(iter2) print
def main(): print print "Demonstration of Grover's Algorithm" print "The OracleGate or V Gate carries the unknown function f(x)" print "> V|x> = ((-1)^f(x))|x> where f(x) = 1 when x = a (True in our case)" print "> and 0 (False in our case) otherwise" print nqubits = 2 print "nqubits = ", nqubits v = OracleGate(nqubits, black_box) print "Oracle or v = OracleGate(%r, black_box)" % nqubits print psi = superposition_basis(nqubits) print "psi:" pprint(psi) demo_vgate_app(v) print "qapply(v*psi)" pprint(qapply(v * psi)) print w = WGate(nqubits) print "WGate or w = WGate(%r)" % nqubits print "On a 2 Qubit system like psi, 1 iteration is enough to yield |1>" print "qapply(w*v*psi)" pprint(qapply(w * v * psi)) print nqubits = 3 print "On a 3 Qubit system, it requires 2 iterations to achieve" print "|1> with high enough probability" psi = superposition_basis(nqubits) print "psi:" pprint(psi) v = OracleGate(nqubits, black_box) print "Oracle or v = OracleGate(%r, black_box)" % nqubits print print "iter1 = grover.grover_iteration(psi, v)" iter1 = qapply(grover_iteration(psi, v)) pprint(iter1) print print "iter2 = grover.grover_iteration(iter1, v)" iter2 = qapply(grover_iteration(iter1, v)) pprint(iter2) print
def test_superposition_of_states(): assert qapply(CNOT(0,1)*HadamardGate(0)*(1/sqrt(2)*Qubit('01') + 1/sqrt(2)*Qubit('10'))).expand() == (Qubit('01')/2 + Qubit('00')/2 - Qubit('11')/2 +\ Qubit('10')/2) assert matrix_to_qubit(represent(CNOT(0,1)*HadamardGate(0)\ *(1/sqrt(2)*Qubit('01') + 1/sqrt(2)*Qubit('10')), nqubits=2))\ == (Qubit('01')/2 + Qubit('00')/2 - Qubit('11')/2 + Qubit('10')/2)
def test_x(): assert X.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity)) assert Commutator(X, Px).doit() == I*hbar assert qapply(X*XKet(x)) == x*XKet(x) assert XKet(x).dual_class() == XBra assert XBra(x).dual_class() == XKet assert (Dagger(XKet(y))*XKet(x)).doit() == DiracDelta(x - y) assert (PxBra(px)*XKet(x)).doit() == \ exp(-I*x*px/hbar)/sqrt(2*pi*hbar) assert represent(XKet(x)) == DiracDelta(x - x_1) assert represent(XBra(x)) == DiracDelta(-x + x_1) assert XBra(x).position == x assert represent(XOp()*XKet()) == x*DiracDelta(x - x_2) assert represent(XOp()*XKet()*XBra('y')) == \ x*DiracDelta(x - x_3)*DiracDelta(x_1 - y) assert represent(XBra("y")*XKet()) == DiracDelta(x - y) assert represent( XKet()*XBra()) == DiracDelta(x - x_2) * DiracDelta(x_1 - x) rep_p = represent(XOp(), basis=PxOp) assert rep_p == hbar*I*DiracDelta(px_1 - px_2)*DifferentialOperator(px_1) assert rep_p == represent(XOp(), basis=PxOp()) assert rep_p == represent(XOp(), basis=PxKet) assert rep_p == represent(XOp(), basis=PxKet()) assert represent(XOp()*PxKet(), basis=PxKet) == \ hbar*I*DiracDelta(px - px_2)*DifferentialOperator(px)
def test_x(): assert X.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity)) assert Commutator(X, Px).doit() == I * hbar assert qapply(X * XKet(x)) == x * XKet(x) assert XKet(x).dual_class() == XBra assert XBra(x).dual_class() == XKet assert (Dagger(XKet(y)) * XKet(x)).doit() == DiracDelta(x - y) assert (PxBra(px)*XKet(x)).doit() == \ exp(-I*x*px/hbar)/sqrt(2*pi*hbar) assert represent(XKet(x)) == DiracDelta(x - x_1) assert represent(XBra(x)) == DiracDelta(-x + x_1) assert XBra(x).position == x assert represent(XOp() * XKet()) == x * DiracDelta(x - x_2) assert represent(XOp()*XKet()*XBra('y')) == \ x*DiracDelta(x - x_3)*DiracDelta(x_1 - y) assert represent(XBra("y") * XKet()) == DiracDelta(x - y) assert represent(XKet() * XBra()) == DiracDelta(x - x_2) * DiracDelta(x_1 - x) rep_p = represent(XOp(), basis=PxOp) assert rep_p == hbar * I * DiracDelta(px_1 - px_2) * DifferentialOperator(px_1) assert rep_p == represent(XOp(), basis=PxOp()) assert rep_p == represent(XOp(), basis=PxKet) assert rep_p == represent(XOp(), basis=PxKet()) assert represent(XOp()*PxKet(), basis=PxKet) == \ hbar*I*DiracDelta(px - px_2)*DifferentialOperator(px)
def test_H(): assert PIABHamiltonian("H").hilbert_space == L2( Interval(S.NegativeInfinity, S.Infinity) ) assert qapply(PIABHamiltonian("H") * PIABKet(n)) == ( n ** 2 * pi ** 2 * hbar ** 2 ) / (2 * m * L ** 2) * PIABKet(n)
def test_jx(): assert Commutator(Jx, Jz).doit() == -I * hbar * Jy assert qapply(Jx * JzKet(1, 1)) == sqrt(2) * hbar * JzKet(1, 0) / 2 assert Jx.rewrite('plusminus') == (Jminus + Jplus) / 2 assert represent(Jx, basis=Jz, j=1) == (represent(Jplus, basis=Jz, j=1) + represent(Jminus, basis=Jz, j=1)) / 2
def test_boson_states(): a = BosonOp("a") # Fock states n = 3 assert (BosonFockBra(0) * BosonFockKet(1)).doit() == 0 assert (BosonFockBra(1) * BosonFockKet(1)).doit() == 1 assert qapply(BosonFockBra(n) * Dagger(a)**n * BosonFockKet(0)) \ == sqrt(prod(range(1, n+1))) # Coherent states alpha1, alpha2 = 1.2, 4.3 assert (BosonCoherentBra(alpha1) * BosonCoherentKet(alpha1)).doit() == 1 assert (BosonCoherentBra(alpha2) * BosonCoherentKet(alpha2)).doit() == 1 assert abs((BosonCoherentBra(alpha1) * BosonCoherentKet(alpha2)).doit() - exp((alpha1 - alpha2) ** 2 * Rational(-1, 2))) < 1e-12 assert qapply(a * BosonCoherentKet(alpha1)) == \ alpha1 * BosonCoherentKet(alpha1)
def test_p(): assert Px.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity)) assert qapply(Px * PxKet(px)) == px * PxKet(px) assert PxKet(px).dual_class == PxBra assert PxBra(x).dual_class == PxKet assert (Dagger(PxKet(py)) * PxKet(px)).doit() == DiracDelta(px - py) assert (XBra(x)*PxKet(px)).doit() ==\ exp(I*x*px/hbar)/sqrt(2*pi*hbar) assert represent(PxKet(px)) == px
def test_boson_states(): a = BosonOp("a") # Fock states n = 3 assert (BosonFockBra(0) * BosonFockKet(1)).doit() == 0 assert (BosonFockBra(1) * BosonFockKet(1)).doit() == 1 assert qapply(BosonFockBra(n) * Dagger(a)**n * BosonFockKet(0)) \ == sqrt(prod(range(1, n+1))) # Coherent states alpha1, alpha2 = 1.2, 4.3 assert (BosonCoherentBra(alpha1) * BosonCoherentKet(alpha1)).doit() == 1 assert (BosonCoherentBra(alpha2) * BosonCoherentKet(alpha2)).doit() == 1 assert abs((BosonCoherentBra(alpha1) * BosonCoherentKet(alpha2)).doit() - exp(-S(1) / 2 * (alpha1 - alpha2) ** 2)) < 1e-12 assert qapply(a * BosonCoherentKet(alpha1)) == \ alpha1 * BosonCoherentKet(alpha1)
def test_p(): assert Px.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity)) assert qapply(Px*PxKet(px)) == px*PxKet(px) assert PxKet(px).dual_class == PxBra assert PxBra(x).dual_class == PxKet assert (Dagger(PxKet(py))*PxKet(px)).doit() == DiracDelta(px-py) assert (XBra(x)*PxKet(px)).doit() ==\ exp(I*x*px/hbar)/sqrt(2*pi*hbar) assert represent(PxKet(px)) == px
def main(): psi = superposition_basis(2) psi # Dense coding demo: # Assume Alice has the left QBit in psi print( "An even superposition of 2 qubits. Assume Alice has the left QBit.") pprint(psi) # The corresponding gates applied to Alice's QBit are: # Identity Gate (1), Not Gate (X), Z Gate (Z), Z Gate and Not Gate (ZX) # Then there's the controlled not gate (with Alice's as control):CNOT(1, 0) # And the Hadamard gate applied to Alice's Qbit: H(1) # To Send Bob the message |0>|0> print("To Send Bob the message |00>.") circuit = H(1) * CNOT(1, 0) result = qapply(circuit * psi) result pprint(result) # To send Bob the message |0>|1> print("To Send Bob the message |01>.") circuit = H(1) * CNOT(1, 0) * X(1) result = qapply(circuit * psi) result pprint(result) # To send Bob the message |1>|0> print("To Send Bob the message |10>.") circuit = H(1) * CNOT(1, 0) * Z(1) result = qapply(circuit * psi) result pprint(result) # To send Bob the message |1>|1> print("To Send Bob the message |11>.") circuit = H(1) * CNOT(1, 0) * Z(1) * X(1) result = qapply(circuit * psi) result pprint(result)
def main(): psi = superposition_basis(2) psi # Dense coding demo: # Assume Alice has the left QBit in psi print "An even superposition of 2 qubits. Assume Alice has the left QBit." pprint(psi) # The corresponding gates applied to Alice's QBit are: # Identity Gate (1), Not Gate (X), Z Gate (Z), Z Gate and Not Gate (ZX) # Then there's the controlled not gate (with Alice's as control):CNOT(1, 0) # And the Hadamard gate applied to Alice's Qbit: H(1) # To Send Bob the message |0>|0> print "To Send Bob the message |00>." circuit = H(1)*CNOT(1, 0) result = qapply(circuit*psi) result pprint(result) # To send Bob the message |0>|1> print "To Send Bob the message |01>." circuit = H(1)*CNOT(1, 0)*X(1) result = qapply(circuit*psi) result pprint(result) # To send Bob the message |1>|0> print "To Send Bob the message |10>." circuit = H(1)*CNOT(1, 0)*Z(1) result = qapply(circuit*psi) result pprint(result) # To send Bob the message |1>|1> print "To Send Bob the message |11>." circuit = H(1)*CNOT(1, 0)*Z(1)*X(1) result = qapply(circuit*psi) result pprint(result)
def test_x(): assert X.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity)) assert Commutator(X, Px).doit() == I*hbar assert qapply(X*XKet(x)) == x*XKet(x) assert XKet(x).dual_class == XBra assert XBra(x).dual_class == XKet assert (Dagger(XKet(y))*XKet(x)).doit() == DiracDelta(x-y) assert (PxBra(px)*XKet(x)).doit() ==\ exp(-I*x*px/hbar)/sqrt(2*pi*hbar) assert represent(XKet(x)) == x assert XBra(x).position == x
def test_x(): assert X.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity)) assert Commutator(X, Px).doit() == I * hbar assert qapply(X * XKet(x)) == x * XKet(x) assert XKet(x).dual_class == XBra assert XBra(x).dual_class == XKet assert (Dagger(XKet(y)) * XKet(x)).doit() == DiracDelta(x - y) assert (PxBra(px)*XKet(x)).doit() ==\ exp(-I*x*px/hbar)/sqrt(2*pi*hbar) assert represent(XKet(x)) == x assert XBra(x).position == x
def test_3dpos(): assert Y.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity)) assert Z.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity)) test_ket = PositionKet3D(x, y, z) assert qapply(X*test_ket) == x*test_ket assert qapply(Y*test_ket) == y*test_ket assert qapply(Z*test_ket) == z*test_ket assert qapply(X*Y*test_ket) == x*y*test_ket assert qapply(X*Y*Z*test_ket) == x*y*z*test_ket assert qapply(Y*Z*test_ket) == y*z*test_ket assert PositionKet3D() == test_ket assert YOp() == Y assert ZOp() == Z assert PositionKet3D.dual_class() == PositionBra3D assert PositionBra3D.dual_class() == PositionKet3D other_ket = PositionKet3D(x_1, y_1, z_1) assert (Dagger(other_ket)*test_ket).doit() == \ DiracDelta(x - x_1)*DiracDelta(y - y_1)*DiracDelta(z - z_1) assert test_ket.position_x == x assert test_ket.position_y == y assert test_ket.position_z == z assert other_ket.position_x == x_1 assert other_ket.position_y == y_1 assert other_ket.position_z == z_1
def test_3dpos(): assert Y.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity)) assert Z.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity)) test_ket = PositionKet3D(x, y, z) assert qapply(X * test_ket) == x * test_ket assert qapply(Y * test_ket) == y * test_ket assert qapply(Z * test_ket) == z * test_ket assert qapply(X * Y * test_ket) == x * y * test_ket assert qapply(X * Y * Z * test_ket) == x * y * z * test_ket assert qapply(Y * Z * test_ket) == y * z * test_ket assert PositionKet3D() == test_ket assert YOp() == Y assert ZOp() == Z assert PositionKet3D.dual_class() == PositionBra3D assert PositionBra3D.dual_class() == PositionKet3D other_ket = PositionKet3D(x_1, y_1, z_1) assert (Dagger(other_ket)*test_ket).doit() == \ DiracDelta(x - x_1)*DiracDelta(y - y_1)*DiracDelta(z - z_1) assert test_ket.position_x == x assert test_ket.position_y == y assert test_ket.position_z == z assert other_ket.position_x == x_1 assert other_ket.position_y == y_1 assert other_ket.position_z == z_1
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
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
def test_pauli_states(): sx, sz = SigmaX(), SigmaZ() up = SigmaZKet(0) down = SigmaZKet(1) assert qapply(sx * up) == down assert qapply(sx * down) == up assert qapply(sz * up) == up assert qapply(sz * down) == -down up = SigmaZBra(0) down = SigmaZBra(1) assert qapply(up * sx, dagger=True) == down assert qapply(down * sx, dagger=True) == up assert qapply(up * sz, dagger=True) == up assert qapply(down * sz, dagger=True) == -down assert Dagger(SigmaZKet(0)) == SigmaZBra(0) assert Dagger(SigmaZBra(1)) == SigmaZKet(1)
def test_pauli_states(): sx, sz = SigmaX(), SigmaZ() up = SigmaZKet(0) down = SigmaZKet(1) assert qapply(sx * up) == down assert qapply(sx * down) == up assert qapply(sz * up) == up assert qapply(sz * down) == - down up = SigmaZBra(0) down = SigmaZBra(1) assert qapply(up * sx, dagger=True) == down assert qapply(down * sx, dagger=True) == up assert qapply(up * sz, dagger=True) == up assert qapply(down * sz, dagger=True) == - down assert Dagger(SigmaZKet(0)) == SigmaZBra(0) assert Dagger(SigmaZBra(1)) == SigmaZKet(1)
def test_p(): assert Px.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity)) assert qapply(Px*PxKet(px)) == px*PxKet(px) assert PxKet(px).dual_class() == PxBra assert PxBra(x).dual_class() == PxKet assert (Dagger(PxKet(py))*PxKet(px)).doit() == DiracDelta(px-py) assert (XBra(x)*PxKet(px)).doit() ==\ exp(I*x*px/hbar)/sqrt(2*pi*hbar) assert represent(PxKet(px)) == DiracDelta(px-px_1) rep_x = represent(PxOp(), basis = XOp) assert rep_x == -hbar*I*DiracDelta(x_1 - x_2)*DifferentialOperator(x_1) assert rep_x == represent(PxOp(), basis = XOp()) assert rep_x == represent(PxOp(), basis = XKet) assert rep_x == represent(PxOp(), basis = XKet()) assert represent(PxOp()*XKet(), basis=XKet) == \ -hbar*I*DiracDelta(x - x_2)*DifferentialOperator(x) assert represent(XBra("y")*PxOp()*XKet(), basis=XKet) == \ -hbar*I*DiracDelta(x-y)*DifferentialOperator(x)
def test_p(): assert Px.hilbert_space == L2(Interval(S.NegativeInfinity, S.Infinity)) assert qapply(Px*PxKet(px)) == px*PxKet(px) assert PxKet(px).dual_class() == PxBra assert PxBra(x).dual_class() == PxKet assert (Dagger(PxKet(py))*PxKet(px)).doit() == DiracDelta(px - py) assert (XBra(x)*PxKet(px)).doit() == \ exp(I*x*px/hbar)/sqrt(2*pi*hbar) assert represent(PxKet(px)) == DiracDelta(px - px_1) rep_x = represent(PxOp(), basis=XOp) assert rep_x == -hbar*I*DiracDelta(x_1 - x_2)*DifferentialOperator(x_1) assert rep_x == represent(PxOp(), basis=XOp()) assert rep_x == represent(PxOp(), basis=XKet) assert rep_x == represent(PxOp(), basis=XKet()) assert represent(PxOp()*XKet(), basis=XKet) == \ -hbar*I*DiracDelta(x - x_2)*DifferentialOperator(x) assert represent(XBra("y")*PxOp()*XKet(), basis=XKet) == \ -hbar*I*DiracDelta(x - y)*DifferentialOperator(x)
def test_rewrite(): # Rewrite to same basis assert JxBra(1, 1).rewrite('Jx') == JxBra(1, 1) assert JxKet(1, 1).rewrite('Jx') == JxKet(1, 1) # Rewriting to different basis assert JxKet(1, 1).rewrite('Jy') == I * JyKet(1, 1) assert JxKet(1, 0).rewrite('Jy') == JyKet(1, 0) assert JxKet(1, -1).rewrite('Jy') == -I * JyKet(1, -1) assert JxKet(1, 1).rewrite( 'Jz') == JzKet(1, 1) / 2 + JzKet(1, 0) / sqrt(2) + JzKet(1, -1) / 2 assert JxKet(1, 0).rewrite( 'Jz') == -sqrt(2) * JzKet(1, 1) / 2 + sqrt(2) * JzKet(1, -1) / 2 assert JxKet(1, -1).rewrite( 'Jz') == JzKet(1, 1) / 2 - JzKet(1, 0) / sqrt(2) + JzKet(1, -1) / 2 assert JyKet(1, 1).rewrite('Jx') == -I * JxKet(1, 1) assert JyKet(1, 0).rewrite('Jx') == JxKet(1, 0) assert JyKet(1, -1).rewrite('Jx') == I * JxKet(1, -1) assert JyKet(1, 1).rewrite('Jz') == JzKet(1, 1) / 2 + sqrt(2) * I * JzKet( 1, 0) / 2 - JzKet(1, -1) / 2 assert JyKet(1, 0).rewrite( 'Jz') == sqrt(2) * I * JzKet(1, 1) / 2 + sqrt(2) * I * JzKet(1, -1) / 2 assert JyKet(1, -1).rewrite('Jz') == -JzKet(1, 1) / 2 + sqrt(2) * I * JzKet( 1, 0) / 2 + JzKet(1, -1) / 2 assert JzKet(1, 1).rewrite( 'Jx') == JxKet(1, 1) / 2 - sqrt(2) * JxKet(1, 0) / 2 + JxKet(1, -1) / 2 assert JzKet(1, 0).rewrite( 'Jx') == sqrt(2) * JxKet(1, 1) / 2 - sqrt(2) * JxKet(1, -1) / 2 assert JzKet(1, -1).rewrite( 'Jx') == JxKet(1, 1) / 2 + sqrt(2) * JxKet(1, 0) / 2 + JxKet(1, -1) / 2 assert JzKet(1, 1).rewrite('Jy') == JyKet(1, 1) / 2 - sqrt(2) * I * JyKet( 1, 0) / 2 - JyKet(1, -1) / 2 assert JzKet(1, 0).rewrite('Jy') == -sqrt(2) * I * JyKet( 1, 1) / 2 - sqrt(2) * I * JyKet(1, -1) / 2 assert JzKet(1, -1).rewrite('Jy') == -JyKet(1, 1) / 2 - sqrt(2) * I * JyKet( 1, 0) / 2 + JyKet(1, -1) / 2 # Innerproducts of rewritten states assert qapply(JxBra(1, 1) * JxKet(1, 1).rewrite('Jy')).doit() == 1 assert qapply(JxBra(1, 0) * JxKet(1, 0).rewrite('Jy')).doit() == 1 assert qapply(JxBra(1, -1) * JxKet(1, -1).rewrite('Jy')).doit() == 1 assert qapply(JxBra(1, 1) * JxKet(1, 1).rewrite('Jz')).doit() == 1 assert qapply(JxBra(1, 0) * JxKet(1, 0).rewrite('Jz')).doit() == 1 assert qapply(JxBra(1, -1) * JxKet(1, -1).rewrite('Jz')).doit() == 1 assert qapply(JyBra(1, 1) * JyKet(1, 1).rewrite('Jx')).doit() == 1 assert qapply(JyBra(1, 0) * JyKet(1, 0).rewrite('Jx')).doit() == 1 assert qapply(JyBra(1, -1) * JyKet(1, -1).rewrite('Jx')).doit() == 1 assert qapply(JyBra(1, 1) * JyKet(1, 1).rewrite('Jz')).doit() == 1 assert qapply(JyBra(1, 0) * JyKet(1, 0).rewrite('Jz')).doit() == 1 assert qapply(JyBra(1, -1) * JyKet(1, -1).rewrite('Jz')).doit() == 1 assert qapply(JyBra(1, 1) * JyKet(1, 1).rewrite('Jz')).doit() == 1 assert qapply(JyBra(1, 0) * JyKet(1, 0).rewrite('Jz')).doit() == 1 assert qapply(JyBra(1, -1) * JyKet(1, -1).rewrite('Jz')).doit() == 1 assert qapply(JzBra(1, 1) * JzKet(1, 1).rewrite('Jy')).doit() == 1 assert qapply(JzBra(1, 0) * JzKet(1, 0).rewrite('Jy')).doit() == 1 assert qapply(JzBra(1, -1) * JzKet(1, -1).rewrite('Jy')).doit() == 1 assert qapply(JxBra(1, 1) * JxKet(1, 0).rewrite('Jy')).doit() == 0 assert qapply(JxBra(1, 1) * JxKet(1, -1).rewrite('Jy')) == 0 assert qapply(JxBra(1, 1) * JxKet(1, 0).rewrite('Jz')).doit() == 0 assert qapply(JxBra(1, 1) * JxKet(1, -1).rewrite('Jz')) == 0 assert qapply(JyBra(1, 1) * JyKet(1, 0).rewrite('Jx')).doit() == 0 assert qapply(JyBra(1, 1) * JyKet(1, -1).rewrite('Jx')) == 0 assert qapply(JyBra(1, 1) * JyKet(1, 0).rewrite('Jz')).doit() == 0 assert qapply(JyBra(1, 1) * JyKet(1, -1).rewrite('Jz')) == 0 assert qapply(JzBra(1, 1) * JzKet(1, 0).rewrite('Jx')).doit() == 0 assert qapply(JzBra(1, 1) * JzKet(1, -1).rewrite('Jx')) == 0 assert qapply(JzBra(1, 1) * JzKet(1, 0).rewrite('Jy')).doit() == 0 assert qapply(JzBra(1, 1) * JzKet(1, -1).rewrite('Jy')) == 0 assert qapply(JxBra(1, 0) * JxKet(1, 1).rewrite('Jy')) == 0 assert qapply(JxBra(1, 0) * JxKet(1, -1).rewrite('Jy')) == 0 assert qapply(JxBra(1, 0) * JxKet(1, 1).rewrite('Jz')) == 0 assert qapply(JxBra(1, 0) * JxKet(1, -1).rewrite('Jz')) == 0 assert qapply(JyBra(1, 0) * JyKet(1, 1).rewrite('Jx')) == 0 assert qapply(JyBra(1, 0) * JyKet(1, -1).rewrite('Jx')) == 0 assert qapply(JyBra(1, 0) * JyKet(1, 1).rewrite('Jz')) == 0 assert qapply(JyBra(1, 0) * JyKet(1, -1).rewrite('Jz')) == 0 assert qapply(JzBra(1, 0) * JzKet(1, 1).rewrite('Jx')) == 0 assert qapply(JzBra(1, 0) * JzKet(1, -1).rewrite('Jx')) == 0 assert qapply(JzBra(1, 0) * JzKet(1, 1).rewrite('Jy')) == 0 assert qapply(JzBra(1, 0) * JzKet(1, -1).rewrite('Jy')) == 0 assert qapply(JxBra(1, -1) * JxKet(1, 1).rewrite('Jy')) == 0 assert qapply(JxBra(1, -1) * JxKet(1, 0).rewrite('Jy')).doit() == 0 assert qapply(JxBra(1, -1) * JxKet(1, 1).rewrite('Jz')) == 0 assert qapply(JxBra(1, -1) * JxKet(1, 0).rewrite('Jz')).doit() == 0 assert qapply(JyBra(1, -1) * JyKet(1, 1).rewrite('Jx')) == 0 assert qapply(JyBra(1, -1) * JyKet(1, 0).rewrite('Jx')).doit() == 0 assert qapply(JyBra(1, -1) * JyKet(1, 1).rewrite('Jz')) == 0 assert qapply(JyBra(1, -1) * JyKet(1, 0).rewrite('Jz')).doit() == 0 assert qapply(JzBra(1, -1) * JzKet(1, 1).rewrite('Jx')) == 0 assert qapply(JzBra(1, -1) * JzKet(1, 0).rewrite('Jx')).doit() == 0 assert qapply(JzBra(1, -1) * JzKet(1, 1).rewrite('Jy')) == 0 assert qapply(JzBra(1, -1) * JzKet(1, 0).rewrite('Jy')).doit() == 0
def test_jz(): assert Commutator(Jz, Jminus).doit() == -hbar * Jminus assert qapply(Jz * JzKet(2, 1)) == hbar * JzKet(2, 1) assert Jz.rewrite('plusminus')
def test_jy(): assert Commutator(Jy, Jz).doit() == I * hbar * Jx assert qapply(Jy * JzKet(1, 1)) == I * sqrt(2) * hbar * JzKet(1, 0) / 2 assert Jy.rewrite('plusminus') == (Jplus - Jminus) / (2 * I) assert represent(Jy, basis=Jz) == (represent(Jplus, basis=Jz) - represent(Jminus, basis=Jz)) / (2 * I)
def test_j2(): j, m = symbols('j m') assert Commutator(J2, Jz).doit() == 0 assert qapply(J2*JzKet(1,1)) == 2*hbar**2*JzKet(1,1) assert qapply(J2*JzKet(j,m)) == j**2*hbar**2*JzKet(j,m)+j*hbar**2*JzKet(j,m) assert J2.matrix_element(1,1,1,1) == 2*hbar**2
def test_jx(): assert Commutator(Jx, Jz).doit() == -I*hbar*Jy assert qapply(Jx*JzKet(1,1)) == sqrt(2)*hbar*JzKet(1,0)/2 assert Jx.rewrite('plusminus') == (Jminus + Jplus)/2 assert represent(Jx, basis=Jz, j=1) == (represent(Jplus, basis=Jz, j=1)+represent(Jminus, basis=Jz, j=1))/2
def test_jminus(): assert qapply(Jminus * JzKet(1, -1)) == 0 assert Jminus.matrix_element(1, 0, 1, 1) == sqrt(2) * hbar assert Jminus.rewrite('xyz') == Jx - I * Jy
def demo_vgate_app(v): for i in range(2**v.nqubits): print 'qapply(v*IntQubit({0}, {1}))'.format(i, v.nqubits) pprint(qapply(v*IntQubit(i, v.nqubits))) qapply(v*IntQubit(i, v.nqubits))
def test_jplus(): assert Commutator(Jplus, Jminus).doit() == 2*hbar*Jz assert qapply(Jplus*JzKet(1,1)) == 0 assert Jplus.matrix_element(1,1,1,1) == 0 assert Jplus.rewrite('xyz') == Jx + I*Jy
def test_jminus(): assert qapply(Jminus*JzKet(1,-1)) == 0 assert Jminus.matrix_element(1,0,1,1) == sqrt(2)*hbar assert Jminus.rewrite('xyz') == Jx - I*Jy
def demo_vgate_app(v): for i in range(2**v.nqubits): print 'qapply(v*IntQubit({0}, {1}))'.format(i, v.nqubits) pprint(qapply(v * IntQubit(i, v.nqubits))) qapply(v * IntQubit(i, v.nqubits))
def demo_vgate_app(v): for i in range(2**v.nqubits): print('qapply(v*IntQubit(%i, %r))' % (i, v.nqubits)) pprint(qapply(v*IntQubit(i, nqubits=v.nqubits))) qapply(v*IntQubit(i, nqubits=v.nqubits))
def test_H(): assert PIABHamiltonian('H').hilbert_space ==\ L2(Interval(S.NegativeInfinity,S.Infinity)) assert qapply(PIABHamiltonian('H')*PIABKet(n)) ==\ (n**2*pi**2*hbar**2)/(2*m*L**2)*PIABKet(n)
def demo_vgate_app(v): for i in range(2**v.nqubits): print('qapply(v*IntQubit(%i, %r))' % (i, v.nqubits)) pprint(qapply(v * IntQubit(i, nqubits=v.nqubits))) qapply(v * IntQubit(i, nqubits=v.nqubits))
def test_jy(): assert Commutator(Jy, Jz).doit() == I*hbar*Jx assert qapply(Jy*JzKet(1,1)) == I*sqrt(2)*hbar*JzKet(1,0)/2 assert Jy.rewrite('plusminus') == (Jplus - Jminus)/(2*I) assert represent(Jy, basis=Jz) == (represent(Jplus, basis=Jz) - represent(Jminus, basis=Jz))/(2*I)
def eval_state(self, state): return qapply(Dagger(state) * self.args[0] * state, dagger=True).doit()
def test_jplus(): assert Commutator(Jplus, Jminus).doit() == 2 * hbar * Jz assert qapply(Jplus * JzKet(1, 1)) == 0 assert Jplus.matrix_element(1, 1, 1, 1) == 0 assert Jplus.rewrite('xyz') == Jx + I * Jy
def test_jz(): assert Commutator(Jz, Jminus).doit() == -hbar*Jminus assert qapply(Jz*JzKet(2,1)) == hbar*JzKet(2,1) assert Jz.rewrite('plusminus')
def test_rewrite(): # Rewrite to same basis assert JxBra(1,1).rewrite('Jx') == JxBra(1,1) assert JxKet(1,1).rewrite('Jx') == JxKet(1,1) # Rewriting to different basis assert JxKet(1,1).rewrite('Jy') == I*JyKet(1,1) assert JxKet(1,0).rewrite('Jy') == JyKet(1,0) assert JxKet(1,-1).rewrite('Jy') == -I*JyKet(1,-1) assert JxKet(1,1).rewrite('Jz') == JzKet(1,1)/2+JzKet(1,0)/sqrt(2)+JzKet(1,-1)/2 assert JxKet(1,0).rewrite('Jz') == -sqrt(2)*JzKet(1,1)/2+sqrt(2)*JzKet(1,-1)/2 assert JxKet(1,-1).rewrite('Jz') == JzKet(1,1)/2-JzKet(1,0)/sqrt(2)+JzKet(1,-1)/2 assert JyKet(1,1).rewrite('Jx') == -I*JxKet(1,1) assert JyKet(1,0).rewrite('Jx') == JxKet(1,0) assert JyKet(1,-1).rewrite('Jx') == I*JxKet(1,-1) assert JyKet(1,1).rewrite('Jz') == JzKet(1,1)/2+sqrt(2)*I*JzKet(1,0)/2-JzKet(1,-1)/2 assert JyKet(1,0).rewrite('Jz') == sqrt(2)*I*JzKet(1,1)/2+sqrt(2)*I*JzKet(1,-1)/2 assert JyKet(1,-1).rewrite('Jz') == -JzKet(1,1)/2+sqrt(2)*I*JzKet(1,0)/2+JzKet(1,-1)/2 assert JzKet(1,1).rewrite('Jx') == JxKet(1,1)/2-sqrt(2)*JxKet(1,0)/2+JxKet(1,-1)/2 assert JzKet(1,0).rewrite('Jx') == sqrt(2)*JxKet(1,1)/2-sqrt(2)*JxKet(1,-1)/2 assert JzKet(1,-1).rewrite('Jx') == JxKet(1,1)/2+sqrt(2)*JxKet(1,0)/2+JxKet(1,-1)/2 assert JzKet(1,1).rewrite('Jy') == JyKet(1,1)/2-sqrt(2)*I*JyKet(1,0)/2-JyKet(1,-1)/2 assert JzKet(1,0).rewrite('Jy') == -sqrt(2)*I*JyKet(1,1)/2-sqrt(2)*I*JyKet(1,-1)/2 assert JzKet(1,-1).rewrite('Jy') == -JyKet(1,1)/2-sqrt(2)*I*JyKet(1,0)/2+JyKet(1,-1)/2 # Innerproducts of rewritten states assert qapply(JxBra(1,1)*JxKet(1,1).rewrite('Jy')).doit() == 1 assert qapply(JxBra(1,0)*JxKet(1,0).rewrite('Jy')).doit() == 1 assert qapply(JxBra(1,-1)*JxKet(1,-1).rewrite('Jy')).doit() == 1 assert qapply(JxBra(1,1)*JxKet(1,1).rewrite('Jz')).doit() == 1 assert qapply(JxBra(1,0)*JxKet(1,0).rewrite('Jz')).doit() == 1 assert qapply(JxBra(1,-1)*JxKet(1,-1).rewrite('Jz')).doit() == 1 assert qapply(JyBra(1,1)*JyKet(1,1).rewrite('Jx')).doit() == 1 assert qapply(JyBra(1,0)*JyKet(1,0).rewrite('Jx')).doit() == 1 assert qapply(JyBra(1,-1)*JyKet(1,-1).rewrite('Jx')).doit() == 1 assert qapply(JyBra(1,1)*JyKet(1,1).rewrite('Jz')).doit() == 1 assert qapply(JyBra(1,0)*JyKet(1,0).rewrite('Jz')).doit() == 1 assert qapply(JyBra(1,-1)*JyKet(1,-1).rewrite('Jz')).doit() == 1 assert qapply(JyBra(1,1)*JyKet(1,1).rewrite('Jz')).doit() == 1 assert qapply(JyBra(1,0)*JyKet(1,0).rewrite('Jz')).doit() == 1 assert qapply(JyBra(1,-1)*JyKet(1,-1).rewrite('Jz')).doit() == 1 assert qapply(JzBra(1,1)*JzKet(1,1).rewrite('Jy')).doit() == 1 assert qapply(JzBra(1,0)*JzKet(1,0).rewrite('Jy')).doit() == 1 assert qapply(JzBra(1,-1)*JzKet(1,-1).rewrite('Jy')).doit() == 1 assert qapply(JxBra(1,1)*JxKet(1,0).rewrite('Jy')).doit() == 0 assert qapply(JxBra(1,1)*JxKet(1,-1).rewrite('Jy')) == 0 assert qapply(JxBra(1,1)*JxKet(1,0).rewrite('Jz')).doit() == 0 assert qapply(JxBra(1,1)*JxKet(1,-1).rewrite('Jz')) == 0 assert qapply(JyBra(1,1)*JyKet(1,0).rewrite('Jx')).doit() == 0 assert qapply(JyBra(1,1)*JyKet(1,-1).rewrite('Jx')) == 0 assert qapply(JyBra(1,1)*JyKet(1,0).rewrite('Jz')).doit() == 0 assert qapply(JyBra(1,1)*JyKet(1,-1).rewrite('Jz')) == 0 assert qapply(JzBra(1,1)*JzKet(1,0).rewrite('Jx')).doit() == 0 assert qapply(JzBra(1,1)*JzKet(1,-1).rewrite('Jx')) == 0 assert qapply(JzBra(1,1)*JzKet(1,0).rewrite('Jy')).doit() == 0 assert qapply(JzBra(1,1)*JzKet(1,-1).rewrite('Jy')) == 0 assert qapply(JxBra(1,0)*JxKet(1,1).rewrite('Jy')) == 0 assert qapply(JxBra(1,0)*JxKet(1,-1).rewrite('Jy')) == 0 assert qapply(JxBra(1,0)*JxKet(1,1).rewrite('Jz')) == 0 assert qapply(JxBra(1,0)*JxKet(1,-1).rewrite('Jz')) == 0 assert qapply(JyBra(1,0)*JyKet(1,1).rewrite('Jx')) == 0 assert qapply(JyBra(1,0)*JyKet(1,-1).rewrite('Jx')) == 0 assert qapply(JyBra(1,0)*JyKet(1,1).rewrite('Jz')) == 0 assert qapply(JyBra(1,0)*JyKet(1,-1).rewrite('Jz')) == 0 assert qapply(JzBra(1,0)*JzKet(1,1).rewrite('Jx')) == 0 assert qapply(JzBra(1,0)*JzKet(1,-1).rewrite('Jx')) == 0 assert qapply(JzBra(1,0)*JzKet(1,1).rewrite('Jy')) == 0 assert qapply(JzBra(1,0)*JzKet(1,-1).rewrite('Jy')) == 0 assert qapply(JxBra(1,-1)*JxKet(1,1).rewrite('Jy')) == 0 assert qapply(JxBra(1,-1)*JxKet(1,0).rewrite('Jy')).doit() == 0 assert qapply(JxBra(1,-1)*JxKet(1,1).rewrite('Jz')) == 0 assert qapply(JxBra(1,-1)*JxKet(1,0).rewrite('Jz')).doit() == 0 assert qapply(JyBra(1,-1)*JyKet(1,1).rewrite('Jx')) == 0 assert qapply(JyBra(1,-1)*JyKet(1,0).rewrite('Jx')).doit() == 0 assert qapply(JyBra(1,-1)*JyKet(1,1).rewrite('Jz')) == 0 assert qapply(JyBra(1,-1)*JyKet(1,0).rewrite('Jz')).doit() == 0 assert qapply(JzBra(1,-1)*JzKet(1,1).rewrite('Jx')) == 0 assert qapply(JzBra(1,-1)*JzKet(1,0).rewrite('Jx')).doit() == 0 assert qapply(JzBra(1,-1)*JzKet(1,1).rewrite('Jy')) == 0 assert qapply(JzBra(1,-1)*JzKet(1,0).rewrite('Jy')).doit() == 0