def test_add_qubit(self): s = StabilizerState() self.assertEqual(s.num_qubits, 0) z0 = StabilizerState([[0, 1]]) s.add_qubit() self.assertTrue(s == z0)
def test_add_qubit_H(self): new_state = [[1, 0]] num = self.eng.add_qubit(new_state) self.assertEqual(num, 0) self.assertEqual(self.eng.activeQubits, 1) self.assertEqual(len(self.eng.qubitReg), 1) state, _ = self.eng.get_register_RI() self.assertEqual(StabilizerState(state), StabilizerState(new_state))
def __init__(self, maxQubits=10): """ Initialize the simple engine. If no number is given for maxQubits, the assumption will be 10. """ super().__init__(maxQubits=maxQubits) self.qubitReg = StabilizerState()
def test_cnot(self): num1 = self.eng.add_fresh_qubit() num2 = self.eng.add_fresh_qubit() self.eng.apply_H(num1) self.eng.apply_CNOT(num1, num2) state, _ = self.eng.get_register_RI() self.assertTrue( StabilizerState(state) == StabilizerState([[1, 1, 0, 0], [0, 0, 1, 1]]))
def test_absorb_this_empty_H(self): eng2 = stabilizerEngine() num = eng2.add_fresh_qubit() eng2.apply_H(num) self.eng.absorb(eng2) self.assertEqual(self.eng.activeQubits, 1) self.assertEqual(len(self.eng.qubitReg), 1) state, _ = self.eng.get_register_RI() self.assertTrue(StabilizerState(state) == StabilizerState([[1, 0]]))
def test_absorb_parts_other_empty(self): num = self.eng.add_fresh_qubit() self.eng.apply_H(num) eng2 = stabilizerEngine() self.eng.absorb_parts(*eng2.get_register_RI(), eng2.activeQubits) self.assertEqual(self.eng.activeQubits, 1) self.assertEqual(len(self.eng.qubitReg), 1) state, _ = self.eng.get_register_RI() self.assertTrue(StabilizerState(state) == StabilizerState([[1, 0]]))
def test_init_of_number_of_qubits(self): s = StabilizerState(1) z0 = StabilizerState([[0, 1]]) self.assertEqual(s.num_qubits, 1) self.assertTrue(s == z0) s = StabilizerState(2) self.assertEqual(s.num_qubits, 2) self.assertTrue(s == z0.tensor_product(z0))
def test_standard_form(self): s1 = StabilizerState([[1, 0, 0, 0], [0, 1, 0, 0]]) s2 = StabilizerState([[1, 0, 0, 0], [1, 1, 0, 0]]) s3 = StabilizerState([[1, 0, 0, 0, 1], [1, 1, 0, 0, 0]]) s4 = StabilizerState([[1, 0, 0, 0, 1], [0, 1, 0, 0, 1]]) self.assertTrue(s1 == s2) self.assertFalse(s1 == s3) self.assertTrue(s3 == s4)
def runClientNode(qReg, virtRoot, myName, classicalNet): """ Code to execute for the local client node. Called if all connections are established. Arguments qReg quantum register (twisted object supporting remote method calls) virtRoot virtual quantum ndoe (twisted object supporting remote method calls) myName name of this node (string) classicalNet servers in the classical communication network (dictionary of hosts) """ logging.debug("LOCAL %s: Runing client side program.", myName) # Create a second register newReg = yield virtRoot.callRemote("add_register") # Create 2 qubits qA = yield virtRoot.callRemote("new_qubit_inreg", qReg) qB = yield virtRoot.callRemote("new_qubit_inreg", newReg) # Put qubits A and B in an EPR state yield qA.callRemote("apply_H") yield qA.callRemote("cnot_onto", qB) if Settings.CONF_BACKEND == "qutip": # Output state (realRho, imagRho) = yield virtRoot.callRemote("get_multiple_qubits", [qA, qB]) rho = assemble_qubit(realRho, imagRho) expectedRho = qp.Qobj([[0.5, 0, 0, 0.5], [0, 0, 0, 0], [0, 0, 0, 0], [0.5, 0, 0, 0.5]]) correct = rho == expectedRho elif Settings.CONF_BACKEND == "projectq": (realvec, imagvec, _, _, _) = yield virtRoot.callRemote("get_register", qA) state = [r + (1j * j) for r, j in zip(realvec, imagvec)] expectedState = [1 / np.sqrt(2), 0, 0, 1 / np.sqrt(2)] correct = np.all(np.isclose(state, expectedState)) elif Settings.CONF_BACKEND == "stabilizer": (array, _, _, _, _) = yield virtRoot.callRemote("get_register", qA) state = StabilizerState(array) expectedState = StabilizerState([[1, 1, 0, 0], [0, 0, 1, 1]]) correct = state == expectedState else: ValueError("Unknown backend {}".format(Settings.CONF_BACKEND)) if correct: print( "Testing register merge, both local, different register............ok" ) else: print( "Testing register merge, both local, different register............fail" ) reactor.stop()
def test_absorb_parts(self): self.eng.add_fresh_qubit() eng2 = stabilizerEngine() eng2.add_fresh_qubit() self.eng.absorb_parts(*eng2.get_register_RI(), eng2.activeQubits) self.assertEqual(self.eng.activeQubits, 2) self.assertEqual(len(self.eng.qubitReg), 2) state, _ = self.eng.get_register_RI() self.assertTrue( StabilizerState(state) == StabilizerState([[0, 0, 1, 0], [0, 0, 0, 1]]))
def test_symplectic_check(self): StabilizerState([[1, 1, 0, 0], [0, 0, 1, 1]]) with self.assertRaises(ValueError): StabilizerState([[1, 0, 0, 0], [0, 0, 1, 0]]) with self.assertRaises(ValueError): StabilizerState([[1, 1, 0, 0], [0, 0, 1, 0]]) with self.assertRaises(ValueError): StabilizerState([[1, 1, 1, 0, 0, 0], [0, 0, 0, 1, 1, 1], [0, 0, 0, 1, 0, 0]])
def test_absorb_parts_EPR(self): eng2 = stabilizerEngine() num1 = eng2.add_fresh_qubit() num2 = eng2.add_fresh_qubit() eng2.apply_H(num1) eng2.apply_CNOT(num1, num2) self.eng.absorb_parts(*eng2.get_register_RI(), eng2.activeQubits) self.assertEqual(self.eng.activeQubits, 2) self.assertEqual(len(self.eng.qubitReg), 2) state, _ = self.eng.get_register_RI() self.assertTrue( StabilizerState(state) == StabilizerState([[1, 1, 0, 0], [0, 0, 1, 1]]))
def got_both(self): """ Recover the qubit from Bob. We should now have a tripartite GHZ state Arguments virtualNum number of the virtual qubit corresponding to the EPR pair received """ logging.debug("LOCAL %s: Got both qubits from Alice and Bob.", self.node.name) # We'll test an operation that will cause a merge of the two remote registers: undo EPR pair yield self.qA.callRemote("cnot_onto", self.qB) yield self.qA.callRemote("apply_H") if Settings.CONF_BACKEND == "qutip": # Output state (realRho, imagRho) = yield self.virtRoot.callRemote("get_multiple_qubits", [self.qA, self.qB]) rho = assemble_qubit(realRho, imagRho) expectedRho = qp.Qobj([[1, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0], [0, 0, 0, 0]]) correct = rho == expectedRho elif Settings.CONF_BACKEND == "projectq": (realvec, imagvec) = yield self.virtRoot.callRemote("get_register_RI", self.qA) state = [r + (1j * j) for r, j in zip(realvec, imagvec)] expectedState = [1, 0, 0, 0] correct = np.all(np.isclose(state, expectedState)) elif Settings.CONF_BACKEND == "stabilizer": (array, _) = yield self.virtRoot.callRemote("get_register_RI", self.qA) state = StabilizerState(array) expectedState = StabilizerState([[0, 0, 1, 0], [0, 0, 0, 1]]) correct = state == expectedState else: ValueError("Unknown backend {}".format(Settings.CONF_BACKEND)) if correct: print( "Testing register merge, both remote, same node, same register............ok" ) else: print( "Testing register merge, both remote, same node, same register............fail" ) reactor.stop()
def remote_receive_qubit(self, virtualNum): """ Recover the qubit from teleportation. Arguments a,b received measurement outcomes from Alice virtualNum number of the virtual qubit corresponding to the EPR pair received """ logging.debug("LOCAL %s: Getting reference to qubit number %d.", self.node.name, virtualNum) # Get a reference to our side of the EPR pair qA = yield self.virtRoot.callRemote("get_virtual_ref", virtualNum) # Create a fresh qubit q = yield self.virtRoot.callRemote("new_qubit_inreg", self.qReg) # Create the GHZ state by entangling the fresh qubit yield qA.callRemote("apply_H") yield qA.callRemote("cnot_onto", q) if Settings.CONF_BACKEND == "qutip": # Output state (realRho, imagRho) = yield self.virtRoot.callRemote("get_multiple_qubits", [qA, q]) rho = assemble_qubit(realRho, imagRho) expectedRho = qp.Qobj([[0.5, 0, 0, 0.5], [0, 0, 0, 0], [0, 0, 0, 0], [0.5, 0, 0, 0.5]]) correct = rho == expectedRho elif Settings.CONF_BACKEND == "projectq": (realvec, imagvec) = yield self.virtRoot.callRemote("get_register_RI", qA) state = [r + (1j * j) for r, j in zip(realvec, imagvec)] expectedState = [1 / np.sqrt(2), 0, 0, 1 / np.sqrt(2)] correct = np.all(np.isclose(state, expectedState)) elif Settings.CONF_BACKEND == "stabilizer": (array, _) = yield self.virtRoot.callRemote("get_register_RI", qA) state = StabilizerState(array) expectedState = StabilizerState([[1, 1, 0, 0], [0, 0, 1, 1]]) correct = state == expectedState else: ValueError("Unknown backend {}".format(Settings.CONF_BACKEND)) if correct: print("Testing register merge: A to B............ok") else: print("Testing register merge: A to B............fail")
def test_correct_init(self): state = StabilizerState([[0, 1]]) self.assertAlmostEqual(state.num_qubits, 1) state = StabilizerState([[0, 1, 0]]) self.assertAlmostEqual(state.num_qubits, 1) state = StabilizerState([[1, 1, 0, 0], [0, 0, 1, 1]]) self.assertAlmostEqual(state.num_qubits, 2) state = StabilizerState([[1, 1, 0, 0, 0], [0, 0, 1, 1, 1]]) self.assertAlmostEqual(state.num_qubits, 2) self.assertTrue(state == StabilizerState(state))
def test_absorb_this_empty_GHZ(self): n = 5 eng2 = stabilizerEngine() qubits = [eng2.add_fresh_qubit() for _ in range(n)] eng2.apply_H(qubits[0]) for i in range(1, n): eng2.apply_CNOT(qubits[0], qubits[i]) self.eng.absorb(eng2) self.assertEqual(self.eng.activeQubits, n) self.assertEqual(len(self.eng.qubitReg), n) state, _ = self.eng.get_register_RI() ref = [1 / np.sqrt(2)] + [0] * (2**n - 2) + [1 / np.sqrt(2)] ref = [[1] * n + [0] * n] for i in range(n - 1): ref += [[0] * n + [0] * i + [1] * 2 + [0] * (n - i - 2)] self.assertTrue(StabilizerState(state) == StabilizerState(ref))
def test_Pauli_phase_tracking(self): S = StabilizerState() self.assertFalse(S.Pauli_phase_tracking([False, False], [False, False])) self.assertFalse(S.Pauli_phase_tracking([True, True], [True, True])) self.assertFalse(S.Pauli_phase_tracking([True, False], [True, False])) self.assertFalse(S.Pauli_phase_tracking([False, True], [False, True])) self.assertTrue(S.Pauli_phase_tracking([True, True], [True, False])) self.assertTrue(S.Pauli_phase_tracking([False, True], [True, True])) self.assertTrue(S.Pauli_phase_tracking([True, False], [False, True])) self.assertEqual(S.Pauli_phase_tracking([True, False], [True, True]), S.Pauli_phase_tracking([True, True], [False, True])) self.assertEqual(S.Pauli_phase_tracking([False, True], [True, False]), S.Pauli_phase_tracking([True, True], [False, True]))
def add_qubit(self, newQubit): """ Add new qubit in the state described by the array containing the generators of the stabilizer group. This should be in the form required by the StabilizerState class. """ try: qubit = StabilizerState(newQubit) except Exception: raise ValueError( "'newQubits' was not in the correct form to be given as an argument to StabilizerState" ) # Create the qubit qubit = StabilizerState(newQubit) num = self.activeQubits self.qubitReg = self.qubitReg.tensor_product(qubit) return num
def test_GHZ(self): n = 5 for _ in range(20): GHZ = StabilizerState(n) GHZ.apply_H(0) for i in range(1, n): GHZ.apply_CNOT(0, i) outcomes = [GHZ.measure(0) for _ in range(n)] self.assertNotIn(False, [outcomes[0] == outcomes[i] for i in range(1, n)])
def test_gaussian_elimination(self): S = StabilizerState(["XZZ", "YIX", "IXX"]) S.put_in_standard_form() self.assertTrue( np.array_equal( S.to_array(), StabilizerState(["+1XZZ", "-1ZYZ", "-1ZZY"]).to_array()))
def test_eq(self): state1 = StabilizerState([[0, 1]]) state2 = StabilizerState([[0, 1]]) state3 = StabilizerState([[0, 1, 0]]) state4 = StabilizerState([[0, 1, 1]]) state5 = StabilizerState([[1, 1, 0]]) state6 = StabilizerState([[1, 1, 0, 0], [0, 0, 1, 1]]) self.assertTrue(state1 == state1) self.assertTrue(state1 == state2) self.assertTrue(state1 == state3) self.assertFalse(state1 == state4) self.assertFalse(state1 == state5) self.assertFalse(state5 == state6)
def absorb_parts(self, R, I, activeQ): """ Absorb the qubits, given in pieces Arguments: R The array describing the stabilizer state (from StabilizerState.to_array) I Unused activeQ active number of qubits """ # Check whether there is space newNum = self.activeQubits + activeQ if newNum > self.maxQubits: raise quantumError( "Cannot merge: qubits exceed the maximum available.\n") try: self.qubitReg = self.qubitReg.tensor_product(StabilizerState(R)) except Exception as err: print(err) print("R: {}".format(R)) print("I: {}".format(I))
def test_tensor_product(self): s1 = StabilizerState([[0, 1]]) # The state |0> s2 = StabilizerState([[0, 1]]) # The state |0> s3 = s1 * s2 # This is then the state |00> s4 = StabilizerState([[0, 0, 1, 0], [0, 0, 0, 1]]) self.assertEqual(s3.num_qubits, 2) self.assertTrue(s3 == s4) s1 = StabilizerState([[0, 1]]) # The state |0> s2 = StabilizerState([[0, 1, 1]]) # The state |1> s3 = s1 * s2 # This is then the state |01> s4 = StabilizerState([[0, 0, 1, 0, 0], [0, 0, 0, 1, 1]]) self.assertEqual(s3.num_qubits, 2) self.assertTrue(s3 == s4)
def test_list_of_str_init(self): phip = StabilizerState([[1, 1, 0, 0], [0, 0, 1, 1]]) phim = StabilizerState([[1, 1, 0, 0, 0], [0, 0, 1, 1, 1]]) data = ["XX", "ZZ"] s1 = StabilizerState(data) self.assertTrue(s1 == phip) data = ["+1XX", "+1ZZ"] s1 = StabilizerState(data) self.assertTrue(s1 == phip) data = ["+1XX", "-1ZZ"] s1 = StabilizerState(data) self.assertTrue(s1 == phim) # Test faulty input data = ["XX", "-1ZZ"] with self.assertRaises(ValueError): StabilizerState(data) data = ["XX", "ZZZ"] with self.assertRaises(ValueError): StabilizerState(data)
def test_networkx_init(self): n = 5 G = nx.complete_graph(n) graph_state = StabilizerState(G) self.assertEqual(graph_state.num_qubits, n) # Create a star graph state and check that this is SQC equiv to the GHZ state G = nx.star_graph(n - 1) graph_state = StabilizerState(G) for i in range(1, n): graph_state.apply_H(i) GHZ_state = StabilizerState(n) GHZ_state.apply_H(0) for i in range(1, n): GHZ_state.apply_CNOT(0, i) self.assertTrue(graph_state == GHZ_state)
class stabilizerEngine(Engine): """ Basic quantum engine which uses stabilizer formalism. Thus only Clifford operations can be performed Attributes: maxQubits: maximum number of qubits this engine will support. """ def __init__(self, maxQubits=10): """ Initialize the simple engine. If no number is given for maxQubits, the assumption will be 10. """ super().__init__(maxQubits=maxQubits) self.qubitReg = StabilizerState() @property def activeQubits(self): return self.qubitReg.num_qubits def add_fresh_qubit(self): """ Add a new qubit initialized in the \|0\> state. """ # Check if we are still allowed to add qubits if self.activeQubits >= self.maxQubits: raise noQubitError("No more qubits available in register.") num = self.activeQubits # Prepare a clean qubit state in |0> self.qubitReg.add_qubit() return num def add_qubit(self, newQubit): """ Add new qubit in the state described by the array containing the generators of the stabilizer group. This should be in the form required by the StabilizerState class. """ try: qubit = StabilizerState(newQubit) except Exception: raise ValueError( "'newQubits' was not in the correct form to be given as an argument to StabilizerState" ) # Create the qubit qubit = StabilizerState(newQubit) num = self.activeQubits self.qubitReg = self.qubitReg.tensor_product(qubit) return num def remove_qubit(self, qubitNum): """ Removes the qubit with the desired number qubitNum """ if (qubitNum + 1) > self.activeQubits: raise quantumError("No such qubit to remove") self.measure_qubit(qubitNum) def get_register_RI(self): """ Retrieves the entire register in real and imaginary part. Twisted only likes to send real valued lists, not complex ones. Since this is in stabilizer formalism the real part will be the boolean matrix describing the generators and the imaginary part will be None """ Re = self.qubitReg.to_array().tolist() Im = None return Re, Im def apply_H(self, qubitNum): """ Applies a Hadamard gate to the qubits with number qubitNum. """ self.qubitReg.apply_H(qubitNum) def apply_K(self, qubitNum): """ Applies a K gate to the qubits with number qubitNum. Maps computational basis to Y eigenbasis. """ self.qubitReg.apply_K(qubitNum) def apply_X(self, qubitNum): """ Applies a X gate to the qubits with number qubitNum. """ self.qubitReg.apply_X(qubitNum) def apply_Z(self, qubitNum): """ Applies a Z gate to the qubits with number qubitNum. """ self.qubitReg.apply_Z(qubitNum) def apply_Y(self, qubitNum): """ Applies a Y gate to the qubits with number qubitNum. """ self.qubitReg.apply_Y(qubitNum) def apply_T(self, qubitNum): """ Applies a T gate to the qubits with number qubitNum. """ raise AttributeError("Cannot apply T gate in stabilizer formalism") def apply_rotation(self, qubitNum, n, a): """ Applies a rotation around the axis n with the angle a to qubit with number qubitNum. If n is zero a ValueError is raised. Arguments: qubitNum Qubit number n A tuple of three numbers specifying the rotation axis, e.g n=(1,0,0) a The rotation angle in radians. """ raise AttributeError( "Cannot apply arbitrary rotation gate in stabilizer formalism") def apply_CNOT(self, qubitNum1, qubitNum2): """ Applies the CNOT to the qubit with the numbers qubitNum1 and qubitNum2. """ self.qubitReg.apply_CNOT(qubitNum1, qubitNum2) def apply_CPHASE(self, qubitNum1, qubitNum2): """ Applies the CPHASE to the qubit with the numbers qubitNum1 and qubitNum2. """ self.qubitReg.apply_CZ(qubitNum1, qubitNum2) def apply_onequbit_gate(self, gate, qubitNum): """ Applies a unitary gate to the specified qubit. Arguments: gate The project Q gate to be applied qubitNum the number of the qubit this gate is applied to """ raise AttributeError( "Cannot apply arbitrary one qubit gate in stabilizer formalism") def apply_twoqubit_gate(self, gate, qubit1, qubit2): """ Applies a unitary gate to the two specified qubits. Arguments: gate The project Q gate to be applied qubit1 the first qubit qubit2 the second qubit """ raise AttributeError( "Cannot apply arbitrary two qubit gate in stabilizer formalism") def measure_qubit_inplace(self, qubitNum): """ Measures the desired qubit in the standard basis. This returns the classical outcome. The quantum register is in the post-measurment state corresponding to the obtained outcome. Arguments: qubitNum qubit to be measured """ # Check we have such a qubit... if (qubitNum + 1) > self.activeQubits: raise quantumError("No such qubit to be measured.") outcome = self.qubitReg.measure(qubitNum, inplace=True) # return measurement outcome return outcome def measure_qubit(self, qubitNum): """ Measures the desired qubit in the standard basis. This returns the classical outcome and deletes the qubit. Arguments: qubitNum qubit to be measured """ outcome = self.qubitReg.measure(qubitNum, inplace=False) return outcome def replace_qubit(self, qubitNum, state): """ Replaces the qubit at position qubitNum with the one given by state. """ raise NotImplementedError( "Currently you cannot replace a qubit using stabilizer formalism") def absorb(self, other): """ Absorb the qubits from the other engine into this one. This is done by tensoring the state at the end. """ # Check whether there is space newNum = self.activeQubits + other.activeQubits if newNum > self.maxQubits: raise quantumError( "Cannot merge: qubits exceed the maximum available.\n") self.qubitReg = self.qubitReg.tensor_product(other.qubitReg) def absorb_parts(self, R, I, activeQ): """ Absorb the qubits, given in pieces Arguments: R The array describing the stabilizer state (from StabilizerState.to_array) I Unused activeQ active number of qubits """ # Check whether there is space newNum = self.activeQubits + activeQ if newNum > self.maxQubits: raise quantumError( "Cannot merge: qubits exceed the maximum available.\n") try: self.qubitReg = self.qubitReg.tensor_product(StabilizerState(R)) except Exception as err: print(err) print("R: {}".format(R)) print("I: {}".format(I))
def test_Z(self): num = self.eng.add_fresh_qubit() self.eng.apply_H(num) self.eng.apply_Z(num) state, _ = self.eng.get_register_RI() self.assertTrue(StabilizerState(state) == StabilizerState([[1, 0, 1]]))
def test_get_register_RI(self): self.eng.add_fresh_qubit() self.eng.add_fresh_qubit() state, _ = self.eng.get_register_RI() self.assertTrue(StabilizerState(state) == StabilizerState(2))
def test_faulty_init(self): with self.assertRaises(ValueError): StabilizerState([1]) with self.assertRaises(ValueError): StabilizerState([[[1]]])
def test_init_of_class(self): state = StabilizerState([[1, 1, 0, 0], [0, 0, 1, 1]]) self.assertTrue(state == StabilizerState(state))