def SimulateRandomClifford(circ, gauge, noise): # Compute the entanglement fidelity between the noisy circuit and the perfect circuit. # 1. Compute the Choi state for the perfect circuit. # 2. Compute the Choi state for the imperfect circuit. # 3. Take the inner product. start = time.time() perfect = qc.Circuit(title='Perfect Circuit') RandomCliffordCircuit(perfect, circ, gauge, 0) pdm = SparseDM(perfect.get_qubit_names()) perfect.apply_to(pdm) #print("Perfect Choi state") #print(sdmc.full_dm.dm.ravel()) noisy = qc.Circuit(title='Noisy Circuit') RandomCliffordCircuit(noisy, circ, gauge, noise) ndm = SparseDM(noisy.get_qubit_names()) noisy.apply_to(ndm) fidelity = np.dot(pdm.full_dm.dm.ravel(), ndm.full_dm.dm.ravel()) # print("fidelity = %g"% (fidelity)) end = time.time() # print("gauge setting\n%s\ndone in %d seconds."% (np.array_str(gauge), end - start)) return fidelity
def test_majority_vote_classical_prob(self): bits = [1, 2, 3] sdm = SparseDM(bits) sdm.hadamard(1) sdm.hadamard(2) sdm.hadamard(3) p0 = 0.42 sdm.classical_probability = p0 result = {b: 0 for b in bits} p = sdm.majority_vote(result) assert np.allclose(p, 0.5 * p0) sdm.hadamard(3) p = sdm.majority_vote(result) assert np.allclose(p, 0.75 * p0) p = sdm.majority_vote({1: 0, 2: 1, 3: 0}) assert np.allclose(p, 0.75 * p0)
def test_peak_on_ground_state(): sdm = SparseDM(1) sdm.ensure_dense(0) p0, p1 = sdm.peak_measurement(0) assert p0 == 1 assert p1 == 0
def test_set_bit(): sdm = SparseDM(10) sdm.set_bit(0, 1) assert sdm.classical[0] == 1 sdm.hadamard(0) sdm.hadamard(0)
def test_multiple_measurement_hadamard_on_classical(self): sdm = SparseDM(2) sdm.hadamard(0) meas = sdm.peak_multiple_measurements([0, 1]) assert len(meas) == 2 assert meas == [({0: 0, 1: 0}, 0.5), ({0: 1, 1: 0}, 0.5)]
def test_majority_vote_invalid_results_error(self): bits = [1, 2, 3] sdm = SparseDM(bits) with pytest.raises(ValueError): sdm.majority_vote({1: "bla"}) with pytest.raises(ValueError): sdm.majority_vote({1: 2})
def test_ensure_dense_simple(): sdm = SparseDM(10) sdm.ensure_dense(0) sdm.ensure_dense(1) assert len(sdm.classical) == 8 assert len(sdm.idx_in_full_dm) == 2 assert sdm.full_dm.no_qubits == 2 assert np.allclose(sdm.trace(), 1)
def test_majority_vote_on_excited_classical(self): bits = [1, 2, 3] sdm = SparseDM(bits) sdm.set_bit(1, 1) sdm.set_bit(3, 1) p = sdm.majority_vote(bits) assert np.allclose(p, 1) assert sdm._last_majority_vote_mask == 0
def test_mix_direct_and_deferred(self): sdm = SparseDM(1) sdm.rotate_y(0, np.pi) sdm.apply_ptm(0, ptm.amp_ph_damping_ptm(1, 0)) sdm.rotate_y(0, np.pi) sdm.project_measurement(0, 0) assert not np.allclose(sdm.trace(), 1)
def test_peak_on_hadamard(): sdm = SparseDM(1) hadamard = ptm.hadamard_ptm() sdm.apply_ptm(0, hadamard) p0, p1 = sdm.peak_measurement(0) assert np.allclose(p0, 0.5) assert np.allclose(p1, 0.5)
def test_majority_vote_on_excited_quantum(self): bits = [1, 2, 3] sdm = SparseDM(bits) sdm.rotate_y(1, np.pi) sdm.rotate_y(2, np.pi) sdm.rotate_y(3, 2 * np.pi) p = sdm.majority_vote(bits) assert np.allclose(p, 1) assert sdm._last_majority_vote_mask == 7
def test_rotate_xyz(): sdm = SparseDM(1) sdm.rotate_x(0, np.pi / 2) sdm.rotate_z(0, np.pi / 2) sdm.rotate_y(0, -np.pi / 2) assert np.allclose(sdm.trace(), 1) sdm.project_measurement(0, 0) assert np.allclose(sdm.trace(), 1)
def test_renormalize(): sdm = SparseDM(2) sdm.hadamard(0) sdm.project_measurement(0, 1) assert np.allclose(sdm.trace(), 0.5) sdm.renormalize() assert np.allclose(sdm.trace(), 1)
def test_majority_vote_gs_classical(self): bits = [1, 2, 3] sdm = SparseDM(bits) assert sdm._last_majority_vote_array is None assert sdm._last_majority_vote_mask is None p = sdm.majority_vote(bits) assert sdm._last_majority_vote_mask == 0 assert np.allclose(p, 0)
def test_meas_on_ground_state(): sdm = SparseDM(1) sdm.ensure_dense(0) sdm.project_measurement(0, 0) assert len(sdm.classical) == 1 assert 0 in sdm.classical assert sdm.classical[0] == 0 assert len(sdm.idx_in_full_dm) == 0 assert sdm.full_dm.no_qubits == 0 assert np.allclose(sdm.trace(), 1)
def test_multiple_measurement_classical_prob(self): sdm = SparseDM(3) sdm.hadamard(0) sdm.hadamard(1) sdm.classical_probability = 0.7 r = sdm.peak_multiple_measurements([0, 1, 2]) total_prob = sum(prob for outcome, prob in r) assert total_prob == sdm.trace()
def test_multiple_measurement_gs(self): sdm = SparseDM(3) sdm.ensure_dense(0) sdm.ensure_dense(1) sdm.ensure_dense(2) meas = sdm.peak_multiple_measurements([0, 1, 2]) assert len(meas) == 8 for state, p in meas: if state[0] == 0 and state[1] == 0 and state[2] == 0: assert np.allclose(p, 1) else: assert np.allclose(p, 0)
def test_copy(): sdm = SparseDM(5) sdm.classical.update({1: 1, 3: 1}) sdm.ensure_dense(2) assert sdm.full_dm.no_qubits == 1 assert len(sdm.classical) == 4 sdm_copy = sdm.copy() assert len(sdm_copy.classical) == 4 assert sdm_copy.classical == {0: 0, 1: 1, 3: 1, 4: 0} assert sdm_copy.classical is not sdm.classical assert sdm.full_dm is not sdm_copy.full_dm assert np.allclose(sdm.full_dm.to_array(), sdm_copy.full_dm.to_array())
def test_deferred_apply(self): sdm = SparseDM(1) assert 0 in sdm.classical p = ptm.hadamard_ptm() sdm.apply_ptm(0, p) assert 0 in sdm.classical assert 0 in sdm.single_ptms_to_do sdm.combine_and_apply_single_ptm(0) assert 0 not in sdm.classical assert 0 not in sdm.single_ptms_to_do
def test_multiple_does_not_change(self): sdm = SparseDM(3) sdm.hadamard(0) sdm.hadamard(1) sdm.ensure_dense(0) sdm.ensure_dense(1) sdm.ensure_dense(2) before = sdm.full_dm.to_array() assert before.shape == (8, 8) sdm.peak_multiple_measurements([0, 1, 2]) assert np.allclose(before, sdm.full_dm.to_array())
def test_meas_on_hadamard(): sdm = SparseDM(1) sdm.hadamard(0) p0, p1 = sdm.peak_measurement(0) assert p0 == 0.5 assert p1 == 0.5 sdm.project_measurement(0, 1) print(sdm.full_dm.to_array()) assert len(sdm.classical) == 1 assert sdm.full_dm.no_qubits == 0 assert sdm.classical[0] == 1 assert np.allclose(sdm.trace(), 0.5)
def test_majority_after_hadamard(self): bits = [1, 2, 3] sdm = SparseDM(bits) sdm.hadamard(1) sdm.hadamard(2) sdm.hadamard(3) p = sdm.majority_vote(bits) assert np.allclose(p, 0.5) sdm.hadamard(3) p = sdm.majority_vote(bits) assert np.allclose(p, 0.25)
def test_multiple_measurement_hadamard_order2_regression(self): sdm = SparseDM(3) sdm.hadamard(0) sdm.hadamard(1) sdm.ensure_dense(0) sdm.ensure_dense(1) sdm.ensure_dense(2) meas = sdm.peak_multiple_measurements([0, 1, 2]) assert len(meas) == 8 for state, p in meas: print(meas) if state[2] == 0: assert np.allclose(p, 0.25) else: assert np.allclose(p, 0)
def test_rotate_x(): sdm = SparseDM(2) sdm.rotate_x(0, np.pi) sdm.rotate_x(1, np.pi) assert np.allclose(sdm.trace(), 1) sdm.project_measurement(1, 1) sdm.project_measurement(0, 1) assert np.allclose(sdm.trace(), 1) sdm.rotate_x(0, np.pi / 2) sdm.rotate_x(1, np.pi / 2) sdm.project_measurement(1, 1) sdm.project_measurement(0, 1) assert np.allclose(sdm.trace(), 0.25)
def test_multiple_measurement_hadamard_order1(self): sdm = SparseDM(3) sdm.hadamard(0) sdm.hadamard(2) sdm.ensure_dense(0) sdm.ensure_dense(1) sdm.ensure_dense(2) meas = sdm.peak_multiple_measurements([0, 1, 2]) assert len(meas) == 8 for state, p in meas: for x in state.values(): assert x in [0, 1] if state[1] == 0: assert np.allclose(p, 0.25) else: assert np.allclose(p, 0)
def test_peak_then_measure(): sdm = SparseDM(1) assert np.allclose(sdm.trace(), 1) sdm.ensure_dense(0) assert np.allclose(sdm.trace(), 1) p0, p1 = sdm.peak_measurement(0) assert np.allclose(p0, 1) assert np.allclose(p1, 0) sdm.project_measurement(0, 0) assert len(sdm.classical) == 1 assert 0 in sdm.classical assert sdm.classical[0] == 0 assert len(sdm.idx_in_full_dm) == 0 assert sdm.full_dm.no_qubits == 0 assert np.allclose(sdm.trace(), 1)
def test_make_imperfect_bell(self): qubit_list = ['swap', 'cp'] with pytest.warns(UserWarning): # We did not provide any seed setup = quick_setup(qubit_list) b = Builder(setup) b.add_gate('RotateY', ['swap'], angle=np.pi/2) b.add_gate('RotateY', ['cp'], angle=np.pi/2) b.add_gate('CZ', ['cp', 'swap']) b.add_gate('RotateY', ['cp'], angle=-np.pi/2) b.finalize() bell_circuit = b.circuit bell_state = SparseDM(bell_circuit.get_qubit_names()) bell_circuit.apply_to(bell_state) diag = np.diag(bell_state.full_dm.to_array()) assert np.abs(diag[0]-0.5) < 1e-2 assert np.abs(diag[3]-0.5) < 1e-2 assert np.abs(diag[1]) < 3e-2 assert np.abs(diag[2]) < 3e-2
def test_peak_on_decay(): sdm = SparseDM(1) sdm.classical[0] = 1 p0, p1 = sdm.peak_measurement(0) assert np.allclose(p0, 0) assert np.allclose(p1, 1) sdm.amp_ph_damping(0, 0.02, 0) p0, p1 = sdm.peak_measurement(0) assert np.allclose(p0, 0.02) assert np.allclose(p1, 0.98) sdm.amp_ph_damping(0, 0.02, 0) p0, p1 = sdm.peak_measurement(0) assert np.allclose(p0, 0.02 + 0.98 * 0.02)
def test_override(self): qubit_list = ['swap', 'cp'] with pytest.warns(UserWarning): # We did not provide any seed setup = quick_setup(qubit_list, noise_flag=False) b = Builder(setup) b < ('RotateY', 'swap', np.pi/2) b < ('RotateY', 'cp', np.pi/2) b < ('CZ', 'cp', 'swap') b < ('RotateY', 'cp', -np.pi/2) b.finalize() bell_circuit = b.circuit bell_state = SparseDM(bell_circuit.get_qubit_names()) bell_circuit.apply_to(bell_state) diag = np.diag(bell_state.full_dm.to_array()) assert np.abs(diag[0]-0.5) < 1e-10 assert np.abs(diag[3]-0.5) < 1e-10 assert np.abs(diag[1]) < 1e-10 assert np.abs(diag[2]) < 1e-10
def test_majority_vote_after_hadamard_inverted(self): bits = [1, 2, 3] sdm = SparseDM(bits) sdm.hadamard(1) sdm.hadamard(2) sdm.hadamard(3) result = {b: 0 for b in bits} p = sdm.majority_vote(result) assert np.allclose(p, 0.5) sdm.hadamard(3) p = sdm.majority_vote(result) assert np.allclose(p, 0.75) p = sdm.majority_vote({1: 0, 2: 1, 3: 0}) assert np.allclose(p, 0.75)