def test_phase_shift_3_pi2_P3_H3(self): # Programming project 2 # Repeat previous test with 3 qbits # Phase Shift 3 QBits Theta = pi/2 P3 H3 Psi num_qbits = 3 theta = pi / 2 phase_shift = Register(num_qbits, self.num_measures) phase_shift.unit_vector[1] = 1. phase_shift.phase_gate(3, theta) states = phase_shift.counting_states() self.assertEqualDictionaryWrapper( states, {"|001>": 1.}, "Phase shift half pi then Hadamard gate same qbit in 3 qbits probability" )
def test_cat_states(self): # Programming project 1 # Cat states test # Set the initial state to the cat state, Eq. (7). # Now, at random, either all of the qbits should be 0 or all of the qbits should be 1 num_qbits = 3 cat_register = Register(num_qbits=num_qbits, num_measures=self.num_measures) cat_register.unit_vector[ 0] = ROOT2RECIPRICOL # initializes the first state cat_register.unit_vector[cat_register.number_of_states - 1] = ROOT2RECIPRICOL states = cat_register.counting_states() test_states = {"|000>": 0.50, "|111>": 0.50} self.assertReasonablyEqualDictionaryWrapper( states, test_states, self.state_accuracy_percent, "Incorrect cat state")
def test_simple_register_states(self, ): # Programming project 1 # Simple register test # test measure of simple states num_qbits = 3 simple_register = Register(num_qbits, self.num_measures) # Set the initial state |W> to one of the basis states, for example, |011> [Eq. (6)]. # With this initial state, every measurement should give the result |011>. for i in range(simple_register.number_of_states): # Test measurement of each single state if i > 0: simple_register.unit_vector[i - 1] = 0.0 simple_register.unit_vector[i] = 1.0 states = simple_register.counting_states() self.assertEqualDictionaryWrapper( states, {self.make_state_string(i, num_qbits): 1.0}, "Invalid state measurement")
def test_full_superposition(self): # Programming project 1 # Test all states equally likely # Set the initial state to an equal superposition of all 2N basis states, # With this state the measured value for each qbit is both random and uncorrelated with the other qbits. # Thus, all possible results from j000i to j111i should occur, each with equal frequency to within # statistical fluctuations. num_qbits = 3 full_register = Register(num_qbits=num_qbits, num_measures=self.num_measures) full_register.unit_vector = [ 1. / sqrt(full_register.number_of_states) ] * full_register.number_of_states # initializes all states probabilities = full_register.counting_states() expected_prob = 1.0 / full_register.number_of_states for probability in probabilities.values(): self.assertReasonablyEqualWrapper(probability, expected_prob, self.state_accuracy_percent, "Bad probability")
def test_pauli_z_gate_single(self): # Test application of single Pauli X (NOT) gate num_qbits = 3 test_vector = [ None, [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [0.0, -1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ] test_states = [None, {"|001>": 1.0}, {"|001>": 1.0}, {"|001>": 1.0}] for i in range(1, num_qbits + 1): pauliz = Register(num_qbits=num_qbits, num_measures=self.num_measures) pauliz.unit_vector[1] = 1. pauliz.pauli_z_gate(i) states = pauliz.counting_states() self.assertEqualWrapper( pauliz.unit_vector, test_vector[i], "Incorrect unit vector after Pauli X " + str(i)) self.assertReasonablyEqualDictionaryWrapper( states, test_states[i], self.state_accuracy_percent, "Incorrect single Pauli X gate probability")
def test_hadamard_gate_single(self): # Programming project 2 # Test application of single Hadamard gate # A Hadamard gate is applied to qbit 2. # From Eq. (14), this puts qbit 2 in an equal superposition |0> and |1>. # Therefore, the result of the calculation should vary randomly between the two possibilities, |000> and |010>. num_qbits = 3 test_vector = [ None, [ROOT2RECIPRICOL, 0.0, 0.0, 0.0, ROOT2RECIPRICOL, 0.0, 0.0, 0.0], [ROOT2RECIPRICOL, 0.0, ROOT2RECIPRICOL, 0.0, 0.0, 0.0, 0.0, 0.0], [ROOT2RECIPRICOL, ROOT2RECIPRICOL, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] ] test_states = [ None, { "|000>": 0.5, "|100>": 0.5 }, { "|000>": 0.5, "|010>": 0.5 }, { "|000>": 0.5, "|001>": 0.5 } ] for i in range(1, num_qbits + 1): hadamard = Register(num_qbits=num_qbits, num_measures=self.num_measures) hadamard.unit_vector[0] = 1. hadamard.hadamard_gate(i) states = hadamard.counting_states() self.assertEqualWrapper( hadamard.unit_vector, test_vector[i], "Incorrect unit vector after Hadamard " + str(i)) self.assertReasonablyEqualDictionaryWrapper( states, test_states[i], self.state_accuracy_percent, "Incorrect single Hadamard gate probability")