def test_measurement_Zbasis(self): print("----------Test measurement Z basis----------") rho = qt.bell_state('00') * qt.bell_state('00').dag() measurement, rho = random_measure_single_Zbasis(rho, 2, 0, True) if measurement == 1: rho_ref = qt.basis(2, 0) * qt.basis(2, 0).dag() if measurement == -1: rho_ref = qt.basis(2, 1) * qt.basis(2, 1).dag() self.assertEqual(rho, rho_ref)
def _choi_state_ket(self, N): # Create a bipartite state. Used in the C-J isomorphism. state = qt.bell_state('00') for i in range(1, N): state = qt.tensor(qt.bell_state('00'), state) # Permute to obtain the desired order of the qubits order = list(range(0, 2*N, 2)) + list(range(1, 2*N, 2)) state = state.permute(order) return state
def generate_noisy_pair(F): a = qt.bell_state('00') * qt.bell_state('00').dag() b = qt.bell_state('01') * qt.bell_state('01').dag() \ + qt.bell_state('10') * qt.bell_state('10').dag() \ + qt.bell_state('11') * qt.bell_state('11').dag() W = F * a + (1 - F) / 3. * b H = qt.snot(2, 1) return H * W * H.dag()
def epsilonj(j): n = int(2 * j) epsilons = qt.tensor(*[np.sqrt(2) * qt.bell_state("11")] * (n)) epsilons = epsilons.permute( list(range(0, 2 * n, 2)) + list(range(1, 2 * n, 2))) S = sym_spin(n) return repair(qt.tensor(S, S) * epsilons)
def test_bell_state(): states = [ qutip.bell_state('00'), qutip.bell_state('01'), qutip.bell_state('10'), qutip.bell_state('11') ] exited = qutip.basis([2, 2], [1, 1]) for state, overlap in zip(states, [0.5**0.5, -0.5**0.5, 0, 0]): assert state.norm() == pytest.approx(1.0) assert state.overlap(exited) == pytest.approx(overlap) for state1, state2 in combinations(states, 2): assert state1.overlap(state2) == pytest.approx(0.0) assert qutip.singlet_state() == qutip.bell_state('11')
def bell_pair_click(eta, theta): """ Return a Bell pair generated using the single click protocol. Initial qubits are initialized in the states: sin(theta)|0> + cos(theta)|1> Parameters ----------- eta : (scalar) detection efficiency theta : (scalar) determines how the initial qubits are initialized """ s = np.sin(theta)**2 r = ((1 - eta)*s)/(1 - eta*s) # TODO Check here state = qt.bell_state('10') * qt.bell_state('10').dag() noise = qt.tensor(qt.basis(2, 1), qt.basis(2, 1)) noise = noise * noise.dag() return (1-r)*state + r*noise
def _generate_bell_pair_BK(self): # Generate a Bell pair using the Barret-Kok protocol. # For this case set the initial state to be |+> # Probaility of success s = .5 r = (1 - self.eta) * s / (1 - self.eta * s) p_success = (1 - r) * self.eta**2 # This circuit number of steps attempts = self._success_number_of_attempts(p_success) + 1 time = self.time_lookup["bell_pair"] * attempts # Update check self.check["bell_pair"] += 1 # Generate Bell pair with F=1 bell = qt.bell_state('10') * qt.bell_state('10').dag() # Apply noisy 1q X to transform state bell = errs.single_qubit_gate(bell, qt.rx(np.pi, 2, 0), self.ps, 2, 0) return time, bell
class TestConcurrence: @pytest.mark.parametrize("dm", [ pytest.param(qutip.bell_state(x).proj(), id='bell' + x) for x in ['00', '01', '10', '11'] ]) def test_maximally_entangled(self, dm): assert abs(qutip.concurrence(dm) - 1) < 1e-12 @pytest.mark.repeat(10) def test_nonzero(self): dm = qutip.rand_dm(4, dims=[[2, 2], [2, 2]]) assert qutip.concurrence(dm) >= 0
def bell_pair_psi(p): """ Return a noisy |phi+> Bell pair. Parameters ----------- p : (scalar) error rate """ a = qt.bell_state('10') * qt.bell_state('10').dag() b = qt.bell_state('01') * qt.bell_state('01').dag() \ + qt.bell_state('00') * qt.bell_state('00').dag() \ + qt.bell_state('11') * qt.bell_state('11').dag() W = (1-p)*a + p/3.*b # H = qt.snot(2, 1) # return H*W*H.dag() return W
def test_measurement_circuit(self): qc = _measurement_circuit() simulators = _simulators_sv(qc) labels = ["00", "01", "10", "11"] for label in labels: state = bell_state(label) for i, simulator in enumerate(simulators): simulator.run(state) if label[0] == "0": assert simulator.cbits[0] == simulator.cbits[1] else: assert simulator.cbits[0] != simulator.cbits[1]
def quantum_strategy(initial_state: qt.Qobj) -> Strategy: shared_system = Simulator(capacity=2) # <1> shared_system.register_state = initial_state your_qubit = shared_system.allocate_qubit() # <2> eve_qubit = shared_system.allocate_qubit() shared_system.register_state = qt.bell_state() # <3> your_angles = [90 * np.pi / 180, 0] # <4> eve_angles = [45 * np.pi / 180, 135 * np.pi / 180] def you(your_input: int) -> int: your_qubit.ry(your_angles[your_input]) # <5> return your_qubit.measure() # <6> def eve(eve_input: int) -> int: # <7> eve_qubit.ry(eve_angles[eve_input]) return eve_qubit.measure() return you, eve # <8>
def generate_werner(n=8, start=0, end=1, step=10, a=None, b=None): """Generate a series of werner state datasets Args: n: number of measurements (default 8) start: starting werner state parameter (default 0) end: ending werner state parameter (default 1) step: number of datasets to be generated (default 10) a: list of vectors for Alice (default None) b: list of vectors for Bob (default None) """ iden = 1 / 4 * qt.tensor(qt.identity(2), qt.identity(2)) bell = qt.ket2dm(qt.bell_state('11')) count = 0 # Check if vectors are passed. If not, generate randomly if a == None or b == None: a, b = random_joint_vectors(n) for w in np.linspace(start, end, step): state = w * bell + (1 - w) * iden filename = 'datasets\\dataset_werner_state_' + str(count) + '.csv' generate_dataset_from_vectors(state, a, b).to_csv(filename) count += 1
def p_ref(f): return (f**2 + 2 * f * (1 - f) / 3 + 5 * ((1 - f) / 3)**2) def single_selection(F1, F2): F = F1 * F2 + (1 - F1) * (1 - F2) / 9 F = F / (F1 * F2 + F1 * (1 - F2) / 3 + F2 * (1 - F1) / 3 + 5 * (1 - F1) * (1 - F2) / 9) return F ### Purification protocols for psi print("--------------------PSI--------------------") ref = qt.bell_state('10') ref2 = qt.bell_state('11') rho_i = errs.bell_pair_phi(p) print("Initial F: ", qt.fidelity(ref, rho_i)**2) rho_i = qt.tensor(rho_i, rho_i) # Apply two_qubit_gates CNOT1 = qt.cnot(4, 2, 0) CNOT2 = qt.cnot(4, 3, 1) GATE = CNOT1 * CNOT2 # CPHASE1 = qt.cphase(np.pi, 4, 0, 2) # CPHASE2 = qt.cphase(np.pi, 4, 1, 3) # GATE = CPHASE1 * CPHASE2 rho = GATE * rho_i * GATE.dag()
def generate_werner(F): a = qt.bell_state('00') a = a * a.dag() return F * a + (1 - F) / 4. * qt.qeye(4)
def calc_overlap(rho, state): bell_states = ['00', '01', '11', '10'] Bell = qt.bell_state(bell_states[state]) Bell.dims = [[4], [1]] return (Bell.dag() * qt.Qobj(rho) * Bell)[0, 0]
""" import qutip as qt import numpy as np import stabilizer # Determine parameters ps = 0.006 pm = 0.006 pg = 0.006 # Initialize objects stab = stabilizer.Stabilizer(ps, pm, pg) stab_ideal = stabilizer.Stabilizer(0, 0, 0) print("------------------TEST SWAP PAIR--------------------------") pair = qt.bell_state('00') * qt.bell_state('00').dag() pair = qt.tensor(pair, qt.basis(2, 0) * qt.basis(2, 0).dag()) pair_swapped = stab._swap_pair(pair, [0, 1]) print(pair) print(pair_swapped) print(qt.fidelity(pair, pair_swapped)**2) print("------------------TEST SWAP GHZ--------------------------") ghz = qt.ghz_state(4) * qt.ghz_state(4).dag() ghz_swapped = stab._swap_ghz(ghz) # print(ghz_swapped) print(qt.fidelity(ghz, ghz_swapped)**2)
def main(): qubits = ['A', 'B1', 'B2', 'C'] nodes = {'A': ['A'], 'B': ['B1', 'B2'], 'C': ['C']} state = proj(q.tensor(q.bell_state(), q.bell_state())) network = qnet(qubits, nodes, state) network.draw()
# # print("-----------P SUCCESS DOUBLE SELECTION----------") # rho = prot.generate_bell_pair() # # Generate raw bell pair # rho = qt.tensor(rho, prot.generate_bell_pair()) # rho = qt.tensor(rho, prot.generate_bell_pair()) # N = len(rho.dims[0]) # # # Apply first set of two qubit gates # CNOT = qt.cnot(N, 2, 0) * qt.cnot(N, 3, 1) # rho = CNOT * rho * CNOT.dag() # # Second set # CNOT = qt.cnot(N, 4, 2) * qt.cnot(N, 5, 3) # rho = CNOT * rho * CNOT.dag() # # p = ps.double_sel(rho, N, [2, 3], [4,5]) # print(p) ps = 0.0 pg = 0.001 pm = 0.0005 pn = 0.07 prot = pr.Protocols(ps=ps, pm=pm, pg=pg, pn=pn) rho_ideal = qt.bell_state("10") rho = errs.bell_pair_psi(pn) print(qt.fidelity(rho, rho_ideal)**2) rho2 = prot.generate_epl() print(qt.fidelity(rho2, rho_ideal)**2)
def env_error_rate(t, a): # Function to calculate the error to the enviroment for step of stabilizers # measurements x = a * t p_env = (1 - np.exp(-x)) / 4. return p_env # Number of iterations for a average iterations = 2000 ignore_number = int(iterations / 100 * 5.) # Initialize objects and define references bell_ref = qt.bell_state('00') * qt.bell_state('00').dag() bell_ref2 = qt.bell_state('01') * qt.bell_state('01').dag() ghz4_ref = qt.ghz_state(4) * qt.ghz_state(4).dag() ghz3_ref = qt.ghz_state(3) * qt.ghz_state(3).dag() rho_ref = bell_ref # Stabilizer and error modeling stuff stab_size = 4 parity = "X" stab = stabilizer.Stabilizer(ps=ps, pm=pm, pg=pg) model = noise_modeling.NoiseModel(stab_size, parity) model.separate_basis_parity() # Choi state for noise noise modeling
import qutip as qt import protocols_old import error_models as errs import tools.operations as ops import tools.p_success as ps rho = errs.bell_pair(.4) rho_ideal = qt.bell_state("00") # print(de.fidelity(rho, rho_ideal)) prot = protocols_old.Protocols(0.0, 0.0075, 0.0075, .1) prot_perf = protocols_old.Protocols( 0., 0., 0., 0., ) print("------------------SINGLE SELECTION-------------------") single = prot.single_selection(rho, [0, 1], "Z") # print(single) print(qt.fidelity(single, rho_ideal)) print("------------------ONE DOT-------------------") one_dot = prot.one_dot(rho, [0, 1], "Z") # print(one_dot) print(qt.fidelity(one_dot, rho_ideal)) print("------------------TWO DOTS-------------------") two_dot = prot.two_dots(rho, [0, 1], "X") # print(two_dot)
pm = 0.009 pg = 0.009 a0 = 3. a1 = 1/80. eta = 1/100. theta = .24 # Initialize objects cb = circuit_block.Blocks(ps, pm, pg, eta, a0, a1, theta) cb_ideal = circuit_block.Blocks(0, 0, 0, 1, 0, 0, np.pi/4) print("------------------PROTOCOL 1-------------------") epl = circuit.Circuit(a0=a0, a1=a1, circuit_block=cb.start_epl) rho, c = epl.run() print("Initial F", qt.fidelity(rho, qt.bell_state('00'))) print(c) print("------------------PROTOCOL 2-------------------") epl = circuit.Circuit(a0=a0, a1=a1, circuit_block=cb.start_epl) circ = circuit.Circuit(a0=a0, a1=a1, circuit_block=epl.run_parallel) rho, c = circ.run() print(rho) print(c) # _, _, rho = cb.start_epl() # H = qt.snot(2, 0) * qt.snot(2, 1)
return np.array([qt.expect(qt.sigmax(), dm),\ qt.expect(qt.sigmay(), dm),\ qt.expect(qt.sigmaz(), dm)]) def disp_subsystems(state): for i in range(len(state.dims[0])): p = state.ptrace(i) print("%d: e: %.3f | %s\n%s" % (i, qt.entropy_vn(p), to_xyz(p), p.full())) print() ################################################################################################ state = qt.tensor(qt.basis(2, 0), qt.bell_state("00")) ################################################################################################ n = len(state.dims[0]) d = state.dims[0][0] j = (d - 1) / 2 dt = 0.01 H = qt.tensor(qt.identity(d), qt.rand_herm(d**(n - 1))) H.dims = [state.dims[0], state.dims[0]] U = (-1j * H * dt).expm() states = [pov(state, i) for i in range(n)] ################################################################################################
Fi = .9 State_initial = generate_noisy_pair(Fi) State = qt.tensor(State_initial, State_initial) CNOT = qt.cnot(4, 0, 2) * qt.cnot(4, 3, 1) State = CNOT * State * CNOT.dag() H = qt.snot(4, 1) * qt.snot(4, 3) State = H * State * H.dag() m1, State_collapsed1 = operations.measure_single(State, 4, 2) m2, State_collapsed = operations.measure_single(State_collapsed1, 3, 2) print(m1, m2) State_final = qt.snot(2, 1) * State_collapsed * qt.snot(2, 1).dag() State_ref = qt.snot(2, 1) * qt.bell_state('00') State_ref = State_ref * State_ref.dag() # initial Fidelity F_initial = (State_ref * State_initial).tr() F_final = (State_ref * State_final).tr() def single_selection(F1, F2): F = F1 * F2 + (1 - F1) * (1 - F2) / 9 F = F / (F1 * F2 + F1 * (1 - F2) / 3 + F2 * (1 - F1) / 3 + 5 * (1 - F1) * (1 - F2) / 9) return F print(single_selection(Fi, Fi), F_final)
""" Test routines for the functions in error_models.py author: Eduardo Villasenor created-on: 05/06/17 """ import unittest import qutip as qt import error_models as errs import matplotlib.pyplot as plt import numpy as np # Define variables for tests psi_test = qt.bell_state("00") rho_test = psi_test * psi_test.dag() t = np.linspace(0, 1.5) ket = qt.rand_ket(2) rho = ket * ket.dag() f = [] for i in t: rho_out = errs.env_error_single(rho, 8, 1/30, i) f += [qt.fidelity(rho, rho_out)**2] plt.plot(t, f) plt.show() class TestErrorModels(unittest.TestCase): def test_two_qubit_error(self): print("----------Test two qubit error-----------")
def initialize(state): bellPhi = qt.bell_state('00') return qt.tensor(state, bellPhi)
eta = 1 / 100 theta = .63 # NOTE: Parameters as in Raja's thesis # ps = 0.006 # pm = 0.006 # pg = 0.006 # a0 = 83.33 # a1 = 1/3. # eta = (0.1)*(0.03)*(0.8) # theta = .24 iterations = 30 # Initialize objects rho_ref = qt.bell_state('00') * qt.bell_state('00').dag() ghz_ref = qt.ghz_state(4) * qt.ghz_state(4).dag() F = [] T = [] print("-------------------PROTOCOL TEST------------------") for i in range(iterations): circuit = protocols.ghz4_epl(ps, pm, pg, eta, a0, a1, theta) rho, operations = circuit.run() fidelity = qt.fidelity(rho, ghz_ref) # print("F: ", fidelity) # print("T: ", operations["time"]) # print(operations) # print(operations) F += [fidelity]
def initialize(state): return qt.composite(state, qt.bell_state(state='11'))
def test_bell_type(): "State CSR Type: bell_state" for k in ['00', '01', '10', '11']: st = bell_state(k) assert_equal(isspmatrix_csr(st.data), True)
n = 9 print("loading...") q = 2*n total_q = 2*n + 2 H = rand_scrambler(n) H.dims = [[2]*n, [2]*n] HL, HV = H.eigenstates() TFD = make_TFD(H, beta=beta) LBACK = qt.tensor((1j*H*tL).expm(), qt.identity(2**n)) # Evolve left qubits backwards in time #LBACK = qt.tensor(U.dag(), qt.identity(2**n)) LBACK.dims = [[2]*q, [2]*q] TFD2 = LBACK*TFD # Insert message ref_msg = qt.bell_state('00') TFD3 = qt.tensor(ref_msg, TFD2) #pairs = [(1+i, 1+i+m) for i in range(m)] #TFD4 = qt.tensor_swap(TFD3, *pairs) TFD4 = TFD3.permute([i if i != 1 and i != 2 else 2 if i == 1 else 1 for i in range(q+2)]) #print([i if i != 1 and i != 2 else 2 if i == 1 else 1 for i in range(q+2)]) # Evolve left qubits forward in time LFORWARD = qt.tensor(qt.identity(2), qt.identity(2**m), (-1j*H*tL).expm(), qt.identity(2**n)) #LFORWARD = qt.tensor(qt.identity(2), qt.identity(2**m), U, qt.identity(2**n)) LFORWARD.dims = [[2]*(q+m+1), [2]*(q+m+1)] TFD5 = LFORWARD*TFD4 # Couple c = n-m cind = [(2*m + i +1, 2*m+n+i +1) for i in range(c)] terms = [] for carrier_pair in cind: