def test_measure(self): # First, test measurement of a system in a pure state STATE = '01010' N = len(STATE) q = QubitSystem(N, int('0b' + STATE, 2)) self.assertEqual(q.measure(), int('0b' + STATE, 2)) self.assertEqual(q.smeasure(), STATE) for i in range(N): self.assertEqual(q.measure(i + 1), int(STATE[i])) # Test probabilistic measurement: repeatedly measure superpositions of # basis states and test that the outcome varies randomly. N = 2 NTRIALS = 100 # number of 1's in the first and second bit, respectively nsuccess1 = 0 nsuccess2 = 0 for i in range(NTRIALS): q = QubitSystem(N) # set the qubits to |00> hadamard_gate(N) * q # equal superposition of four basis states nsuccess1 += q.measure(1) nsuccess2 += q.measure(2) # Test that repeated measurement gives the same result state = q.measure() self.assertEqual(q.measure(), state) self.assertEqual(q.measure(), state) # Test that the number of 1's that appeared in the first and second # qubits is approximately half the total (within approx. 99% confidence # interval) CUTOFF = NTRIALS * 0.15 self.assertTrue(abs(nsuccess1 - NTRIALS / 2.) < CUTOFF) self.assertTrue(abs(nsuccess2 - NTRIALS / 2.) < CUTOFF)