def __init__(self, matrix: np.ndarray, display_name: str = "U"): verify_quantum_operator_matrix_dimensions(matrix) self._matrix = np.array(matrix, dtype=complex) qubit_count = int(np.log2(self._matrix.shape[0])) if not is_unitary(self._matrix): raise ValueError(f"{self._matrix} is not unitary") super().__init__(qubit_count=qubit_count, ascii_symbols=[display_name] * qubit_count)
def check_noise_target_unitary(noise: Noise, target_unitary: np.ndarray): """Helper function to check 1. whether the input matrix is a np.ndarray type; 2. whether the target_unitary is a unitary; Args: noise (Noise): A Noise class object to be applied to the circuit. target_unitary (np.ndarray): matrix of the target unitary gates """ if not isinstance(target_unitary, np.ndarray): raise TypeError("target_unitary must be a np.ndarray type") if not is_unitary(target_unitary): raise ValueError("target_unitary must be a unitary")
def test_is_unitary_exception(): is_unitary(invalid_matrix_type_error)
def test_is_unitary_false(matrix): assert not is_unitary(matrix)
def test_is_unitary_true(): assert is_unitary(valid_unitary_hermitian_matrix)