def __init__(self, utry_target, gate_list): """ CircuitTensor Constructor Args: utry_target (np.ndarray): Unitary target matrix gate_list (list[Gate]): The circuit's gate list. """ if not utils.is_unitary(utry_target): raise TypeError("Specified target matrix is not unitary.") if not isinstance(gate_list, list): raise TypeError("Gate list is not a list.") if not all([isinstance(gate, Gate) for gate in gate_list]): raise TypeError("Gate list contains non-gate objects.") self.utry_target = utry_target self.num_qubits = utils.get_num_qubits(self.utry_target) if not all([ utils.is_valid_location(gate.location, self.num_qubits) for gate in gate_list ]): raise ValueError("Gate location mismatch with circuit tensor.") self.gate_list = gate_list self.reinitialize()
def __init__ ( self, theta, location, fixed = False, check_params = True ): """ Gate Constructor Args: theta (float): The gate's angle of rotation. location (tuple[int]): The qubits this gate is applied to. fixed (bool): True if the gate's unitary operation is immutable. check_params (bool): True implies parameters are checked for correctness. """ if check_params: if not isinstance( theta, float ): raise TypeError( "Invalid theta angle, not a float." ) if not isinstance( location, tuple ): raise TypeError( "Specified location is not valid." ) if not utils.is_valid_location( location ): raise TypeError( "Specified location is not valid." ) if len( location ) != 2: raise TypeError( "Specified location is not valid." ) if not isinstance( fixed, bool ): raise TypeError( "Invalid fixed parameter." ) self.theta = theta self.location = location self.gate_size = len( self.location ) self.fixed = fixed
def __init__(self, utry, location, fixed=False, check_params=True): """ Gate Constructor Args: utry (np.ndarray): The gate's unitary operation. location (tuple[int]): Set of qubits this gate is applied to. fixed (bool): True if the gate's unitary operation is immutable. check_params (bool): True implies parameters are checked for correctness. """ if check_params: if not utils.is_unitary(utry): raise TypeError("Specified matrix is not unitary.") if not utils.is_valid_location(location): raise TypeError("Specified location is not valid.") if len(location) != utils.get_num_qubits(utry): raise ValueError("Location size does not match unitary.") if not isinstance(fixed, bool): raise TypeError("Invalid fixed parameter.") self.utry = utry self.location = location self.gate_size = len(location) self.fixed = fixed
def test_is_valid_location_invalid1(self): self.assertFalse(is_valid_location("a")) self.assertFalse(is_valid_location(0)) self.assertFalse(is_valid_location("a", 256)) self.assertFalse(is_valid_location(0, 256))
def test_is_valid_location_valid(self): self.assertTrue(is_valid_location((0, 1, 2))) self.assertTrue(is_valid_location((0, 1, 2), 3)) self.assertTrue(is_valid_location((0, 1, 2, 17))) self.assertTrue(is_valid_location((0, 1, 2, 17), 18))
def test_is_valid_location_empty(self): self.assertTrue(is_valid_location(tuple())) self.assertTrue(is_valid_location(tuple(), 0)) self.assertTrue(is_valid_location(tuple(), 3))
def test_is_valid_location_invalid5(self): self.assertFalse(is_valid_location((0, 2, 1)))
def test_is_valid_location_invalid4(self): self.assertFalse(is_valid_location((0, 1, 2), 1)) self.assertFalse(is_valid_location((0, 1, 2), 0))
def test_is_valid_location_invalid3(self): self.assertFalse(is_valid_location((0, 0))) self.assertFalse(is_valid_location((0, 0), 256))
def test_is_valid_location_invalid2(self): self.assertFalse(is_valid_location((0, "a", 2))) self.assertFalse(is_valid_location((0, "a", 2), 256))