def test_superposition_basis(): nbits = 2 first_half_state = IntQubit(0, nbits)/2 + IntQubit(1, nbits)/2 second_half_state = IntQubit(2, nbits)/2 + IntQubit(3, nbits)/2 assert first_half_state + second_half_state == superposition_basis(nbits) nbits = 3 firstq = (1/sqrt(8))*IntQubit(0, nbits) + (1/sqrt(8))*IntQubit(1, nbits) secondq = (1/sqrt(8))*IntQubit(2, nbits) + (1/sqrt(8))*IntQubit(3, nbits) thirdq = (1/sqrt(8))*IntQubit(4, nbits) + (1/sqrt(8))*IntQubit(5, nbits) fourthq = (1/sqrt(8))*IntQubit(6, nbits) + (1/sqrt(8))*IntQubit(7, nbits) assert firstq + secondq + thirdq + fourthq == superposition_basis(nbits)
def test_superposition_basis(): nbits = 2 first_half_state = IntQubit(0, nqubits=nbits)/2 + IntQubit(1, nqubits=nbits)/2 second_half_state = IntQubit(2, nbits)/2 + IntQubit(3, nbits)/2 assert first_half_state + second_half_state == superposition_basis(nbits) nbits = 3 firstq = (1/sqrt(8))*IntQubit(0, nqubits=nbits) + (1/sqrt(8))*IntQubit(1, nqubits=nbits) secondq = (1/sqrt(8))*IntQubit(2, nbits) + (1/sqrt(8))*IntQubit(3, nbits) thirdq = (1/sqrt(8))*IntQubit(4, nbits) + (1/sqrt(8))*IntQubit(5, nbits) fourthq = (1/sqrt(8))*IntQubit(6, nbits) + (1/sqrt(8))*IntQubit(7, nbits) assert firstq + secondq + thirdq + fourthq == superposition_basis(nbits)
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_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=nqubits) assert qapply(WGate(nqubits)*IntQubit(1, nqubits=nqubits)) == expected
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
def test_grover(): nqubits = 2 assert apply_grover(return_one_on_one, nqubits) == IntQubit(1, nqubits=nqubits) nqubits = 4 basis_states = superposition_basis(nqubits) expected = (-13*basis_states)/64 + 264*IntQubit(2, nqubits)/256 assert apply_grover(return_one_on_two, 4) == qapply(expected)
def test_grover(): nqubits = 2 assert apply_grover(return_one_on_one, nqubits) == IntQubit(1, nqubits) nqubits = 4 basis_states = superposition_basis(nqubits) expected = (-13*basis_states)/64 + 264*IntQubit(2, nqubits)/256 assert apply_grover(return_one_on_two, 4) == qapply(expected)
def test_grover_iteration_2(): numqubits = 4 basis_states = superposition_basis(numqubits) v = OracleGate(numqubits, return_one_on_two) # After (pi/4)sqrt(pow(2, n)), IntQubit(2) should have highest prob # In this case, after around pi times (3 or 4) iterated = grover_iteration(basis_states, v) iterated = qapply(iterated) iterated = grover_iteration(iterated, v) iterated = qapply(iterated) iterated = grover_iteration(iterated, v) iterated = qapply(iterated) # In this case, probability was highest after 3 iterations # Probability of Qubit('0010') was 251/256 (3) vs 781/1024 (4) # Ask about measurement expected = (-13*basis_states)/64 + 264*IntQubit(2, numqubits)/256 assert qapply(expected) == iterated
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_grover_iteration_1(): numqubits = 2 basis_states = superposition_basis(numqubits) v = OracleGate(numqubits, return_one_on_one) expected = IntQubit(1, nqubits=numqubits) assert qapply(grover_iteration(basis_states, v)) == expected
def test_g_bbht_search(nqubits=5): basis_states = superposition_basis(nqubits) oracle = random_oracle(nqubits, min_img=5, max_img=15, q_type="bin") x = g_bbht_search(basis_states, oracle)[0] assert oracle.search_function(x) == 1
def superposition(self): return superposition_basis(self.qubit.nqubits)
def test_grover_iteration_1(): numqubits = 2 basis_states = superposition_basis(numqubits) v = OracleGate(numqubits, return_one_on_one) expected = IntQubit(1, numqubits) assert qapply(grover_iteration(basis_states, v)) == expected