def test_wstate(self): filename = "w-state.qasm" filepath = Path(__file__).parent / 'qasm_files' / filename qc = read_qasm(filepath) rand_state = rand_ket(2) state = tensor(tensor(basis(2, 0), basis(2, 0), basis(2, 0)), rand_state) fourth = Measurement("test_rand", targets=[3]) _, probs_initial = fourth.measurement_comp_basis(state) simulators = _simulators_sv(qc) for simulator in simulators: result = simulator.run_statistics(state) final_states = result.get_final_states() result_cbits = result.get_cbits() for i, final_state in enumerate(final_states): _, probs_final = fourth.measurement_comp_basis(final_state) np.testing.assert_allclose(probs_initial, probs_final) assert sum(result_cbits[i]) == 1
def test_measurement_collapse(index): """ Test if correct state is created after measurement using the example of the Bell state """ state_00 = tensor(basis(2, 0), basis(2, 0)) state_11 = tensor(basis(2, 1), basis(2, 1)) bell_state = (state_00 + state_11) / sqrt(2) M = Measurement("BM", targets=[index]) states, probabilities = M.measurement_comp_basis(bell_state) np.testing.assert_allclose(probabilities, [0.5, 0.5]) for i, state in enumerate(states): if i == 0: Mprime = Measurement("00", targets=[1 - index]) states_00, probability_00 = Mprime.measurement_comp_basis(state) assert probability_00[0] == 1 assert states_00[1] is None else: Mprime = Measurement("11", targets=[1 - index]) states_11, probability_11 = Mprime.measurement_comp_basis(state) assert probability_11[1] == 1 assert states_11[0] is None
def test_runstatistics_teleportation(self): """ Test circuit run_statistics on teleportation circuit """ teleportation = _teleportation_circuit() final_measurement = Measurement("start", targets=[2]) initial_measurement = Measurement("start", targets=[0]) original_state = tensor(rand_ket(2), basis(2, 0), basis(2, 0)) _, initial_probabilities = initial_measurement.measurement_comp_basis(original_state) teleportation_results = teleportation.run_statistics(original_state) states = teleportation_results.get_final_states() probabilities = teleportation_results.get_probabilities() for i, state in enumerate(states): state_final = state prob = probabilities[i] _, final_probabilities = final_measurement.measurement_comp_basis(state_final) np.testing.assert_allclose(initial_probabilities, final_probabilities) assert prob == pytest.approx(0.25, abs=1e-7) mixed_state = sum(p * ket2dm(s) for p, s in zip(probabilities, states)) dm_state = ket2dm(original_state) teleportation2 = _teleportation_circuit2() final_state = teleportation2.run(dm_state) _, probs1 = final_measurement.measurement_comp_basis(final_state) _, probs2 = final_measurement.measurement_comp_basis(mixed_state) np.testing.assert_allclose(probs1, probs2)
def test_measurement_comp_basis(): """ Test measurements to test probability calculation in computational basis measurments on a 3 qubit state """ qubit_kets = [rand_ket(2), rand_ket(2), rand_ket(2)] qubit_dms = [ket2dm(qubit_kets[i]) for i in range(3)] state = tensor(qubit_kets) density_mat = tensor(qubit_dms) for i in range(3): m_i = Measurement("M" + str(i), i) final_states, probabilities_state = m_i.measurement_comp_basis(state) final_dms, probabilities_dm = m_i.measurement_comp_basis(density_mat) amps = qubit_kets[i].full() probabilities_i = [np.abs(amps[0][0])**2, np.abs(amps[1][0])**2] np.testing.assert_allclose(probabilities_state, probabilities_dm) np.testing.assert_allclose(probabilities_state, probabilities_i) for j, final_state in enumerate(final_states): np.testing.assert_allclose(final_dms[j], ket2dm(final_state))
def test_run_teleportation(self): """ Test circuit run and mid-circuit measurement functionality by repeating the teleportation circuit on multiple random kets """ teleportation = _teleportation_circuit() state = tensor(rand_ket(2), basis(2, 0), basis(2, 0)) initial_measurement = Measurement("start", targets=[0]) _, initial_probabilities = initial_measurement.measurement_comp_basis(state) state_final, probability = teleportation.run(state) final_measurement = Measurement("start", targets=[2]) _, final_probabilities = final_measurement.measurement_comp_basis(state_final) np.testing.assert_allclose(initial_probabilities, final_probabilities)
def test_qasm_teleportation(): filename = "teleportation.qasm" filepath = Path(__file__).parent / 'qasm_files' / filename teleportation = read_qasm(filepath) final_measurement = Measurement("start", targets=[2]) initial_measurement = Measurement("start", targets=[0]) state = tensor(rand_ket(2), basis(2, 0), basis(2, 0)) _, initial_probabilities = initial_measurement.measurement_comp_basis(state) states, probabilites = teleportation.run_statistics(state) for i, state in enumerate(states): final = state prob = probabilites[i] _, final_probabilities = final_measurement.measurement_comp_basis(final) np.testing.assert_allclose(initial_probabilities, final_probabilities) assert prob == pytest.approx(0.25, abs=1e-7)
def test_runstatistics_teleportation(self): """ Test circuit run_statistics on teleportation circuit """ teleportation = _teleportation_circuit() final_measurement = Measurement("start", targets=[2]) initial_measurement = Measurement("start", targets=[0]) state = tensor(rand_ket(2), basis(2, 0), basis(2, 0)) _, initial_probabilities = initial_measurement.measurement_comp_basis(state) states, probabilites = teleportation.run_statistics(state) for i, state in enumerate(states): state_final = state prob = probabilites[i] _, final_probabilities = final_measurement.measurement_comp_basis(state_final) np.testing.assert_allclose(initial_probabilities, final_probabilities) assert prob == pytest.approx(0.25, abs=1e-7)
def test_qasm_teleportation(): filename = "teleportation.qasm" filepath = Path(__file__).parent / 'qasm_files' / filename with pytest.warns(UserWarning, match="not preserved in QubitCircuit"): teleportation = read_qasm(filepath) final_measurement = Measurement("start", targets=[2]) initial_measurement = Measurement("start", targets=[0]) state = tensor(rand_ket(2), basis(2, 0), basis(2, 0)) _, initial_probabilities = initial_measurement.measurement_comp_basis(state) teleportation_results = teleportation.run_statistics(state) states = teleportation_results.get_final_states() probabilities = teleportation_results.get_probabilities() for i, state in enumerate(states): final = state prob = probabilities[i] _, final_probabilities = final_measurement.measurement_comp_basis(final) np.testing.assert_allclose(initial_probabilities, final_probabilities) assert prob == pytest.approx(0.25, abs=1e-7)