def test_get_tensor_format(self): utry = unitary_group.rvs(8) gate = Gate(utry, (0, 1, 2)) tensor = gate.get_tensor_format() self.assertTrue(len(tensor.shape) == 6) self.assertTrue(all([x == 2 for x in tensor.shape])) self.assertTrue(np.allclose(np.reshape(tensor, (8, 8)), utry))
def test_get_tensor_format_right(self): utry = unitary_group.rvs(8) gate = Gate(utry, (0, 1, 2)) tensor = gate.get_tensor_format(compress_right=True) self.assertTrue(len(tensor.shape) == 4) self.assertTrue(all([x == 2 for x in tensor.shape[:-1]])) self.assertTrue(tensor.shape[-1] == 8) self.assertTrue(np.allclose(np.reshape(tensor, (8, 8)), utry))
def test_get_tensor_format_all(self): utry = unitary_group.rvs(8) gate = Gate(utry, (0, 1, 2)) tensor = gate.get_tensor_format(compress_left=True, compress_right=True) self.assertTrue(len(tensor.shape) == 2) self.assertTrue(tensor.shape[0] == 8) self.assertTrue(tensor.shape[1] == 8) self.assertTrue(np.allclose(tensor, utry))
def test_gate_constructor_invalid(self): gate = Gate(self.TOFFOLI, (0, 1, 2)) self.assertRaises(TypeError, CircuitTensor, self.INVALID, [gate]) self.assertRaises(TypeError, CircuitTensor, self.TOFFOLI, ["a", gate]) self.assertRaises(TypeError, CircuitTensor, self.TOFFOLI, [gate, "a"]) self.assertRaises(TypeError, CircuitTensor, self.TOFFOLI, ["a"]) self.assertRaises(TypeError, CircuitTensor, self.TOFFOLI, "a") gate = Gate(self.TOFFOLI, (0, 1, 7)) self.assertRaises(ValueError, CircuitTensor, self.TOFFOLI, [gate])
def test_apply_right(self): u1 = unitary_group.rvs(8) u2 = unitary_group.rvs(4) g = Gate(u2, (0, 1)) ct = CircuitTensor(u1, []) ct.apply_right(g) prod = np.kron(u2, np.identity(2)) @ u1.conj().T prod_test = ct.utry self.assertTrue(np.allclose(prod, prod_test)) ct.apply_right(g) prod = np.kron(u2, np.identity(2)) @ prod prod_test = ct.utry self.assertTrue(np.allclose(prod, prod_test))
def test_gate_constructor_valid(self): gate = Gate(self.TOFFOLI, (0, 1, 2), True) self.assertTrue(np.array_equal(gate.utry, self.TOFFOLI)) self.assertTrue(np.array_equal(gate.location, (0, 1, 2))) self.assertEqual(gate.gate_size, 3) self.assertTrue(gate.fixed)
def test_gate_str_2(self): gate = Gate(self.TOFFOLI, (4, 5, 7)) self.assertEqual(str(gate), str((4, 5, 7)) + ":" + str(self.TOFFOLI))
def test_gate_str_1(self): gate = Gate(self.TOFFOLI, (0, 1, 2)) self.assertEqual(str(gate), str((0, 1, 2)) + ":" + str(self.TOFFOLI))
def test_get_inverse(self): utry = unitary_group.rvs(8) gate = Gate(utry, (0, 1, 2)) inv_gate = gate.get_inverse() self.assertTrue(np.allclose(inv_gate.utry @ gate.utry, np.identity(8))) self.assertTrue(np.allclose(gate.utry @ inv_gate.utry, np.identity(8)))
def test_gate_constructor_valid(self): gate = Gate(self.TOFFOLI, (0, 1, 2)) ct = CircuitTensor(self.TOFFOLI, [gate]) self.assertTrue(np.array_equal(gate.utry, ct.gate_list[0].utry)) self.assertTrue(len(ct.gate_list) == 1) self.assertTrue(np.allclose(ct.utry, np.identity(8)))
def test_gate_repr_2 ( self ): gate = Gate( self.TOFFOLI, (4, 5, 7) ) self.assertEqual( repr( gate ), str( (4, 5, 7) ) + ": [[(1+0j) ... 0j]]" )
def test_gate_repr_1 ( self ): gate = Gate( self.TOFFOLI, (0, 1, 2) ) self.assertEqual( repr( gate ), str( (0, 1, 2) ) + ": [[(1+0j) ... 0j]]" )