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_apply_CZ(self): # Classical CNOT z0x0 = StabilizerState([[0, 0, 1, 0], [0, 1, 0, 0]]) z1x1 = StabilizerState([[0, 0, 1, 0, 1], [0, 1, 0, 0, 1]]) z0x0.apply_X(0) z0x0.apply_CZ(0, 1) z0x0.put_in_standard_form() self.assertTrue(z0x0 == z1x1) # EPR pair z0z0 = StabilizerState([[0, 0, 1, 0], [0, 0, 0, 1]]) epr = StabilizerState([[1, 1, 0, 0], [0, 0, 1, 1]]) z0z0.apply_H(0) # Effective CNOT z0z0.apply_H(1) z0z0.apply_CZ(0, 1) z0z0.apply_H(1) self.assertTrue(z0z0 == epr) # Graph state z0z0 = StabilizerState([[0, 0, 1, 0], [0, 0, 0, 1]]) graph_state = StabilizerState([[1, 0, 0, 1], [0, 1, 1, 0]]) z0z0.apply_H(0) z0z0.apply_H(1) # Effective CPHASE z0z0.apply_CZ(0, 1) self.assertTrue(z0z0 == graph_state)
def test_measure(self): for _ in range(20): z0 = StabilizerState([[0, 1]]) m = z0.measure(0) self.assertEqual(m, 0) for _ in range(20): z1 = StabilizerState([[0, 1, 1]]) m = z1.measure(0) self.assertEqual(m, 1) z0z0 = StabilizerState([[0, 0, 1, 0], [0, 0, 0, 1]]) z1z1 = StabilizerState([[0, 0, 1, 0, 1], [0, 0, 0, 1, 1]]) for _ in range(20): epr = StabilizerState([[1, 1, 0, 0], [0, 0, 1, 1]]) m0 = epr.measure(0, inplace=True) m1 = epr.measure(1, inplace=True) self.assertEqual(m0, m1) if m0 == 0: self.assertTrue(epr == z0z0) else: self.assertTrue(epr == z1z1) outcomes = [] for _ in range(200): x0 = StabilizerState([[1, 0]]) outcomes.append(x0.measure(0)) self.assertTrue(80 <= sum(outcomes) <= 120)
def test_apply_CNOT(self): # Classical CNOT z0z0 = StabilizerState([[0, 0, 1, 0], [0, 0, 0, 1]]) z1z1 = StabilizerState([[0, 0, 1, 0, 1], [0, 0, 0, 1, 1]]) z0z0.apply_X(0) z0z0.apply_CNOT(0, 1) self.assertTrue(z0z0 == z1z1) # EPR pair z0z0 = StabilizerState([[0, 0, 1, 0], [0, 0, 0, 1]]) epr = StabilizerState([[1, 1, 0, 0], [0, 0, 1, 1]]) z0z0.apply_H(0) z0z0.apply_CNOT(0, 1) self.assertTrue(z0z0 == epr) # Graph state z0z0 = StabilizerState([[0, 0, 1, 0], [0, 0, 0, 1]]) graph_state = StabilizerState([[1, 0, 0, 1], [0, 1, 1, 0]]) z0z0.apply_H(0) z0z0.apply_H(1) # Effective CPHASE z0z0.apply_H(1) z0z0.apply_CNOT(0, 1) z0z0.apply_H(1) self.assertTrue(z0z0 == graph_state)
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 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_apply_S(self): z0 = StabilizerState([[0, 1]]) x0 = StabilizerState([[1, 0]]) x1 = StabilizerState([[1, 0, 1]]) y0 = StabilizerState([[1, 1]]) s1 = StabilizerState(z0) s2 = StabilizerState(x0) s1.apply_S(0) self.assertTrue(s1 == z0) s2.apply_S(0) self.assertTrue(s2 == y0) s2.apply_S(0) self.assertTrue(s2 == x1)
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_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_init_of_class(self): state = StabilizerState([[1, 1, 0, 0], [0, 0, 1, 1]]) self.assertTrue(state == StabilizerState(state))
def test_faulty_init(self): with self.assertRaises(ValueError): StabilizerState([1]) with self.assertRaises(ValueError): StabilizerState([[[1]]])
def test_to_array(self): s1 = StabilizerState([[0, 0, 1, 0, 0], [0, 0, 0, 1, 1]]) s2 = StabilizerState(s1.to_array()) self.assertTrue(s1 == s2)
def test_repr(self): s1 = StabilizerState([[0, 0, 1, 0, 0], [0, 0, 0, 1, 1]]) s2 = StabilizerState(eval(repr(s1))) self.assertTrue(s1 == s2)