def test_pauli_expansion_valid_4(self): sigma = get_norder_paulis(4) for H in sigma: alpha = pauli_expansion(H) reH = np.sum([a * p for a, p in zip(alpha, sigma)], 0) self.assertTrue(hilbert_schmidt_distance(H, reH) <= 1e-16)
def test_pauli_expansion_valid_comb(self): sigma = get_norder_paulis(4) sqrt2 = np.sqrt(2) / 2 for H1, H2 in zip(sigma, sigma[1:]): H = sqrt2 * H1 + sqrt2 * H2 alpha = pauli_expansion(H) reH = np.sum([a * p for a, p in zip(alpha, sigma)], 0) self.assertTrue(hilbert_schmidt_distance(H, reH) <= 1e-16)
def test_hilbert_schmidt_distance_numpy_tensor(self): toffoli_tensor = tf.constant(self.TOFFOLI) loss = hilbert_schmidt_distance(self.TOFFOLI, toffoli_tensor) init_op = tf.global_variables_initializer() with tf.Session() as sess: sess.run(init_op) self.assertEquals(loss.eval(), 0)
def test_fixedgate_get_unitary(self): reset_tensor_cache() fg = FixedGate("Test", 4, 2, (0, 1)) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) gate = fg.get_unitary(sess) paulis = get_pauli_n_qubit_projection(4, (0, 1)) H = pauli_dot_product([0.25] * 16, paulis) U = la.expm(1j * H) self.assertTrue(hilbert_schmidt_distance(gate, U) <= 1e-16)
def test_get_unitary_from_pauli_coefs_1 ( self ): sigma = get_norder_paulis( 1 ) for U in sigma: pauli_coefs = pauli_expansion( unitary_log_no_i( U ) ) reU = get_unitary_from_pauli_coefs( pauli_coefs ) self.assertTrue( hilbert_schmidt_distance( U, reU ) <= 1e-16 ) self.assertTrue( np.allclose( U.conj().T @ U, np.identity( len( U ) ), rtol = 0, atol = 1e-16 ) and np.allclose( U @ U.conj().T, np.identity( len( U ) ), rtol = 0, atol = 1e-16 ) )
def test_unitary_log_no_i_eig_valid_4(self): sigma = get_norder_paulis(4) for U in sigma: H = unitary_log_no_i_eig(U) self.assertTrue(np.allclose(H, H.conj().T, rtol=0, atol=1e-15)) reU = la.expm(1j * H) self.assertTrue(hilbert_schmidt_distance(U, reU) <= 1e-16) self.assertTrue( np.allclose( U.conj().T @ U, np.identity(len(U)), rtol=0, atol=1e-16) and np.allclose( U @ U.conj().T, np.identity(len(U)), rtol=0, atol=1e-16))
def test_genericgate_get_unitary(self): reset_tensor_cache() lm = LocationModel(4, 2) gg = GenericGate("Test", 4, 2, lm, loc_vals=[1, 0, 0, 0, 0, 0]) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) gate = gg.get_unitary(sess) paulis = get_pauli_n_qubit_projection(4, (0, 1)) H = pauli_dot_product([0.25] * 16, paulis) U = la.expm(1j * H) self.assertTrue(hilbert_schmidt_distance(gate, U) <= 1e-16)
def test_unitary_log_no_i_valid_comp(self): sigma = get_norder_paulis(4) for U1, U2 in zip(sigma, sigma[1:]): U = U1 @ U2 H = unitary_log_no_i(U) self.assertTrue(np.allclose(H, H.conj().T, rtol=0, atol=1e-15)) reU = la.expm(1j * H) self.assertTrue(hilbert_schmidt_distance(U, reU) <= 1e-16) self.assertTrue( np.allclose( U.conj().T @ U, np.identity(len(U)), rtol=0, atol=1e-16) and np.allclose( U @ U.conj().T, np.identity(len(U)), rtol=0, atol=1e-16))
def test_kak_synthesize_valid(self): qasm = synthesize(self.CNOT) utry = calc_unitary(flip_circ(QuantumCircuit.from_qasm_str(qasm))) self.assertTrue(hilbert_schmidt_distance(self.CNOT, utry) <= 1e-15)
def test_hilbert_schmidt_distance_numpy_numpy(self): loss = hilbert_schmidt_distance(self.TOFFOLI, self.TOFFOLI) self.assertEquals(loss, 0)