def test_schmidt_examples(self): self.assertEqual(qc.bell_state().schmidt_number(indices=[0]), 2) self.assertEqual( qc.positive_superposition(d=2).schmidt_number(indices=[0]), 1) x = (qc.bitstring(0, 0) + qc.bitstring(0, 1) + qc.bitstring(1, 0)) / np.sqrt(3) self.assertEqual(x.schmidt_number(indices=[0]), 2)
def test_bitstring_state_unit_length(self): num_tests = 10 for test_i in range(num_tests): d = np.random.randint(1, 8) bits = np.random.choice([0, 1], size=d) state = qc.bitstring(*bits) diff = abs(state.probabilities.sum() - 1) self.assertLess(diff, epsilon)
def bell_state(x, y): H = qc.Hadamard() CNOT = qc.CNOT() phi = qc.bitstring(x, y) phi = H(phi, qubit_indices=[0]) return CNOT(phi)
def test_positive_superposition_measurement(self): state = qc.positive_superposition() rho1 = qc.DensityOperator.from_ensemble([state]) measurement = rho1.measure()[0] state = qc.bitstring(measurement) rho2 = qc.DensityOperator.from_ensemble([state]) assert_allclose(rho1._t, rho2._t)
def test_bitstring_measurement(self): num_tests = 10 for test_i in range(num_tests): d = np.random.randint(1, 8) bits = tuple(np.random.choice([0, 1], size=d, replace=True)) state = qc.bitstring(*bits) measurement = state.measure() self.assertEqual(bits, measurement)
def test_bell_state_measurement2(self): state = qc.bell_state(0, 0) rho1 = qc.DensityOperator.from_ensemble([state]) idx = np.random.randint(2) m = rho1.measure(qubit_indices=[idx], remove=True)[0] state = qc.bitstring(m) rho2 = qc.DensityOperator.from_ensemble([state]) assert_allclose(rho1._t, rho2._t)
def test_bitstring_kronecker(self): d = 5 for bits in product([0, 1], repeat=d): bitstring = qc.bitstring(*bits) v1 = bitstring.to_column_vector() v2 = np.zeros(2**d) index = 0 for b_i, b in enumerate(bits): index += b * 2**(d - b_i - 1) v2[index] = 1 self.assertLess(max_absolute_difference(v1, v2), epsilon)
def test_computational_basis_state_measurement(self): num_tests = 10 for test_i in range(num_tests): d = np.random.randint(2, 8) bits = np.random.randint(2, size=d) bits_list = list(bits) rho = qc.DensityOperator.from_ensemble([qc.bitstring(*bits_list)]) measurement_d = np.random.randint(1, d + 1) qubit_indices = np.random.choice(d, size=measurement_d, replace=False) measured_bits = rho.measure(qubit_indices) assert_allclose(bits[qubit_indices], np.array(measured_bits))
def test_U_f_basis_measurement(self): num_tests = 10 for test_i in range(num_tests): d = np.random.randint(1, 8) f = random_boolean_function(d) U = qc.U_f(f, d=d+1) bits = tuple(np.random.choice([0, 1], size=d, replace=True)) input_qubits = qc.bitstring(*bits) ans_qubit = qc.zeros() state = input_qubits * ans_qubit state = U(state) answer = f(*bits) measured_ans = state.measure(qubit_indices=d) self.assertEqual(answer, measured_ans)