def test_cx_int(): circuit_1 = Circuit() circuit_1.add(Qubits(2)) circuit_1.add(Hadamard(0)) circuit_1.add(Cx(0, 1)) circuit_1.add(Measure(0, 1)) circuit_2 = Circuit() circuit_2.add(Qubits(2)) circuit_2.add(Hadamard(1)) circuit_2.add(Cx(0, 1)) circuit_2.add(Measure(0, 1)) sess = QSession(backend=QuantumSimulator()) result_1 = sess.run(circuit_1, num_shots=1024) result_2 = sess.run(circuit_2, num_shots=1024) assert result_1['01'] == 0 assert result_1['10'] > 450 assert result_1['00'] > 450 assert result_1['11'] == 0 assert result_2['01'] == 0 assert result_2['10'] == 0 assert result_2['00'] > 450 assert result_2['11'] > 450
def test_teleportation(): circuit = Circuit() circuit.add(Qubits(3)) circuit.add(CNOT(1, 2)) circuit.add(CNOT(0, 1)) circuit.add(Hadamard(0)) circuit.add(Measure([0, 1])) result = circuit.run(1024).result # All 16 states should be relatively equal probability if result["11"] == 1024: circuit.add(PauliX(2)) circuit.add(PauliZ(2)) elif result["10"] == 1024: circuit.add(PauliZ(2)) elif result["01"] == 1024: circuit.add(PauliX(2)) circuit.add(Measure([2])) result2 = circuit.run(1024).result # TODO: Fix this test. assert result2
def find_period(a, N): """WIP: Quantum subroutine for shor's algorithm. Finds the period of a function of the form: f(x) = a^x % N This uses the quantum fourier transform. """ circuit = Circuit() # circuit.add(Qubits(5)) # circuit.add(QFT(0, 1, 2, 3)) # circuit.add(X(4)) # circuit.add(quantum_amod_15(a)) # circuit.add(QFT(0, 1, 2, 3)) circuit.add(Qubits(5)) circuit.add(H(0)).add(H(1)).add(H(2)).add(H(3)) circuit.add(X(4)) circuit.add(quantum_amod_15(a)) circuit.add(QFT(3, 2, 1, 0)) # Inverse Quantum Fourier transform job = circuit.run(1024) result = job.result # TODO: This is still broken, likely due to differences between Qiskit and our library # See: https://github.com/shor-team/shor/issues/39 # from shor.utils.visual import plot_results # plot_results(result) most_common = result.counts.most_common() if len(most_common) > 1: return gcd(most_common[0][0], most_common[1][0]) return 1
def test_u3_int(): circuit_1 = Circuit() circuit_1.add(Qubits(1)) circuit_1.add(U3(0, theta=np.pi / 2, phi=-np.pi / 2, lam=np.pi / 2)) circuit_1.add(Measure([0])) circuit_2 = Circuit() circuit_2.add(Qubits(1)) circuit_2.add(Rx(theta=np.pi)) circuit_2.add(Measure([0])) result_1 = circuit_1.run(1024).result result_2 = circuit_2.run(1024).result assert result_1["0"] > 450 assert result_1["1"] > 450 assert result_2["0"] > 450 assert result_2["1"] > 450
def test_ry_int(): circuit = Circuit() circuit.add(Qubits(1)) circuit.add(Ry(0, angle=np.pi / 2)) circuit.add(Measure([0])) job = circuit.run(1000) result = job.result assert result["0"] > 450 assert result["1"] > 450
def test_u2_int(): circuit_1 = Circuit() circuit_1.add(Qubits(1)) circuit_1.add(U2(0, phi=-np.pi / 2, alpha=np.pi / 2)) circuit_1.add(Measure([0])) result_1 = circuit_1.run(1024).result assert result_1["0"] > 450 assert result_1["1"] > 450
def test_t_integration(): circuit = Circuit() circuit.add(Qubits(1)) circuit.add(T(0)) # Can also use H() circuit.add(Measure([0])) job = circuit.run(1024) result = job.result assert result["0"] == 1024 assert result["1"] == 0
def test_U3_int(): circuit_1 = Circuit() circuit_1.add(Qubits(1)) circuit_1.add(U3(0, theta=np.pi / 2, phi=-np.pi / 2, alpha=np.pi / 2)) circuit_1.add(Measure(0)) circuit_2 = Circuit() circuit_2.add(Qubits(1)) circuit_2.add(Rx(theta=np.pi)) circuit_2.add(Measure(0)) sess = QSession(backend=QuantumSimulator()) result_1 = sess.run(circuit_1, num_shots=1024) result_2 = sess.run(circuit_2, num_shots=1024) assert result_1['0'] > 450 assert result_1['1'] > 450 assert result_2['0'] > 450 assert result_2['1'] > 450
def test_inity_int(): circuit_1 = Circuit() circuit_1.add(Qubits(1)) circuit_1.add(Init_y(0)) circuit_1.add(Measure([0])) result_1 = circuit_1.run(1024).result assert result_1["0"] > 450 assert result_1["1"] > 450
def test_id_qubit(): circuit = Circuit() circuit.add(Qubits(1)) circuit.add(ID(0)) circuit.add(Measure([0])) job = circuit.run(1024) result = job.result # Accounting for random noise, results won't be exact assert result["1"] == 0 assert result["0"] == 1024
def test_u1_integration(): circuit = Circuit() circuit.add(Qubits(1)) circuit.add(PauliX(0)) circuit.add(U1(0)) circuit.add(Measure([0])) job = circuit.run(1024) result = job.result assert result["0"] == 0 assert result["1"] == 1024
def test_pauliy_integration(): circuit = Circuit() circuit.add(Qubits(1)) circuit.add(PauliY(0)) # Can also use H() circuit.add(Measure([0])) job = circuit.run(1024) result = job.result # Accounting for random noise, results won't be exact assert result["0"] == 0 assert result["1"] == 1024
def test_single_qubit(): circuit = Circuit() circuit.add(Qubits(1)) circuit.add(Hadamard(0)) circuit.add(Measure([0])) job = circuit.run(1024, provider=QiskitProvider()) result = job.result # Accounting for random noise, results won't be exact assert result[bin(0)] > 450 assert result[bin(1)] > 450
def test_crz_integration(): circuit = Circuit() circuit.add(Qubits(2)) circuit.add(CRZ(0, 1, angle=math.pi / 3)) circuit.add(Measure(0, 1)) job = circuit.run(1024) result = job.result assert result["11"] == 0 assert result["00"] == 1024 assert result["10"] == 0 assert result["01"] == 0
def test_rz_int(): circuit = Circuit() circuit.add(Qubits(1)) circuit.add(Rz(0, angle=np.pi / 2)) circuit.add(Measure(0)) sess = QSession(backend=QuantumSimulator()) result = sess.run(circuit, num_shots=1000) assert result['0'] == 1000 assert result['1'] == 0
def test_s_integration(): circuit = Circuit() circuit.add(Qubits(1)) circuit.add(S(0)) # Can also use H() circuit.add(Measure()) sess = QSession(backend=QuantumSimulator()) result = sess.run(circuit, num_shots=1024) assert result['0'] == 1024 assert result['1'] == 0
def test_crz_integration(): circuit = Circuit() circuit.add(Qubits(2)) circuit.add(CRZ(0, 1, angle=math.pi / 3)) sess = QSession(backend=QuantumSimulator()) result = sess.run(circuit, num_shots=1024) assert result['11'] == 0 assert result['00'] == 1024 assert result['10'] == 0 assert result['01'] == 0
def test_cr_int(): circuit = Circuit() circuit.add(Qubits(2)) circuit.add(Cr(0, 1, angle=np.pi / 2)) circuit.add(Measure(0, 1)) result = circuit.run(1000).result assert result["00"] == 1000 assert result["01"] == 0 assert result["10"] == 0 assert result["11"] == 0
def test_u1_integration(): circuit = Circuit() circuit.add(Qubits(1)) circuit.add(PauliX(0)) circuit.add(U1(0)) circuit.add(Measure()) sess = QSession(backend=QuantumSimulator()) result = sess.run(circuit, num_shots=1024) assert result['0'] == 0 assert result['1'] == 1024
def test_ID_qubit(): circuit = Circuit() circuit.add(Qubits(1)) circuit.add(ID(0)) circuit.add(Measure()) sess = QSession(backend=QuantumSimulator()) result = sess.run(circuit, num_shots=1024) # Accounting for random noise, results won't be exact assert result['1'] == 0 assert result['0'] == 1024
def test_crk_int(): circuit = Circuit() circuit.add(Qubits(2)) circuit.add(CRk(0, 1, k=2)) circuit.add(Measure(0, 1)) result = circuit.run(1000).result assert result["00"] == 1000 assert result["01"] == 0 assert result["10"] == 0 assert result["11"] == 0
def test_mult_gate_inputs1(): circuit_1 = Circuit() circuit_1.add(Qubits(2)) circuit_1.add(H([0, 1])) circuit_1.add(Measure(0, 1)) result_1 = circuit_1.run(1024).result assert result_1["00"] > 215 assert result_1["11"] > 215 assert result_1["10"] > 215 assert result_1["01"] > 215
def test_swap_integration(): circuit = Circuit() circuit.add(Qubits(2)) circuit.add(PauliX(0)) circuit.add(SWAP(0, 1)) circuit.add(Measure([0, 1])) job = circuit.run(1024) result = job.result assert result["11"] == 0 assert result["00"] == 0 assert result["01"] == 0 assert result["10"] == 1024
def test_entanglement(): circuit = Circuit() circuit.add(Qubits(2)) circuit.add(Hadamard(0)) circuit.add(CNOT(0, 1)) circuit.add(Measure([0, 1])) job = circuit.run(1024) result = job.result assert result["01"] == 0 assert result["10"] == 0 assert result["00"] > 450 assert result["11"] > 450
def test_pauliy_integration(): circuit = Circuit() circuit.add(Qubits(1)) circuit.add(PauliY(0)) # Can also use H() circuit.add(Measure()) sess = QSession(backend=QuantumSimulator()) result = sess.run(circuit, num_shots=1024) # Accounting for random noise, results won't be exact assert result['0'] == 0 assert result['1'] == 1024
def test_multi_hadamard(): circuit = Circuit() circuit.add(Qubits(4)) circuit.add(Hadamard(0)) circuit.add(Hadamard(1)) circuit.add(Hadamard(2)) circuit.add(Hadamard(3)) circuit.add(Measure([0, 1, 2, 3])) job = circuit.run(1024) result = job.result # All 16 states should be relatively equal probability assert len(result.counts) == 16 assert max(result.counts.values()) - min(result.counts.values()) < 50
def test_cz_int(): circuit = Circuit() circuit.add(Qubits(2)) circuit.add(Hadamard(0)) circuit.add(Hadamard(1)) circuit.add(Cz(0, 1)) circuit.add(Measure([0, 1])) job = circuit.run(1000) result = job.result assert result["00"] > 210 assert result["01"] > 210 assert result["10"] > 210 assert result["11"] > 210
def test_unitary_symmetry_does_nothing(): symmetric_circuit_1 = Circuit() symmetric_circuit_1.add(Qubits(2)) symmetric_circuit_1.add(Hadamard(0)) symmetric_circuit_1.add(Hadamard(0)) symmetric_circuit_1.add(CNOT(0, 1)) symmetric_circuit_1.add(CNOT(0, 1)) symmetric_circuit_1.add(Measure([0, 1])) symmetric_circuit_2 = Circuit() symmetric_circuit_2.add(Qubits(2, state=1)) symmetric_circuit_2.add(Hadamard(0)) symmetric_circuit_2.add(CNOT(0, 1)) symmetric_circuit_2.add(CNOT(0, 1)) symmetric_circuit_2.add(Hadamard(0)) symmetric_circuit_2.add(Measure([0, 1])) result_1 = symmetric_circuit_1.run(1024).result result_2 = symmetric_circuit_2.run(1024).result assert result_1.counts.get(0) == 1024 assert result_2.counts.get(0) == 1024
def test_cz2_int(): circuit_1 = Circuit() circuit_1.add(Qubits(2)) circuit_1.add(PauliX(0)) circuit_1.add(Cz(0, 1)) circuit_1.add(Measure(0, 1)) result_1 = circuit_1.run(1024).result assert result_1["00"] == 0 assert result_1["11"] == 0 assert result_1["01"] == 1024 assert result_1["10"] == 0
def test_ch_integration(): circuit = Circuit() circuit.add(Qubits(2)) circuit.add(PauliX(0)) circuit.add(CH(0, 1)) sess = QSession(backend=QuantumSimulator()) result = sess.run(circuit, num_shots=1024) assert result['11'] > 450 assert result['00'] == 0 assert result['10'] > 450 assert result['01'] == 0