Skip to content

Commit

Permalink
adds test for _controlled_qubit_unitary
Browse files Browse the repository at this point in the history
  • Loading branch information
Hosseinberg authored Apr 25, 2023
1 parent 3efa677 commit a399d36
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions tests/test_apply.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,23 @@ def test_qubit_unitary(self, init_state, mat, tol):
expected = mat @ state
assert np.allclose(res, expected, tol)

@pytest.mark.parametrize("control_wires", [[0], [1], [0, 1]])
@pytest.mark.parametrize("mat", [U, U2])
def test_controlled_qubit_unitary(self, init_state, mat, control_wires, tol):
"""Test ControlledQubitUnitary application"""

N = int(np.log2(len(mat)))
dev = QulacsDevice(N + len(control_wires))
state = init_state(N + len(control_wires))

op = qml.ControlledQubitUnitary(mat, wires=list(range(N, N + len(control_wires) + 1)), control_wires=control_wires)
dev.apply([qml.QubitStateVector(state, wires=list(range(N + len(control_wires)))), op])
dev._obs_queue = []

res = dev.state
expected = np.kron(np.eye(2 ** N), np.eye(2 ** len(control_wires))) @ np.kron(np.eye(2 ** N), mat) @ state
assert np.allclose(res, expected, tol)

def test_invalid_qubit_state_unitary(self):
"""Test that an exception is raised if the
unitary matrix is the wrong size"""
Expand Down

0 comments on commit a399d36

Please sign in to comment.