예제 #1
0
def test_V():
    """Test for the _make_V function"""
    dim = 4

    V_expected = -np.eye(dim)
    V_expected[1, 1] = V_expected[3, 3] = 1
    V = _make_V(dim)

    assert np.allclose(V, V_expected)
예제 #2
0
def test_apply_controlled_v(n_wires):
    """Test if the _apply_controlled_v performs the correct transformation by reconstructing the
    unitary and comparing against the one provided in _make_V."""
    n_all_wires = n_wires + 1

    wires = Wires(range(n_wires))
    control_wire = Wires(n_wires)

    circ = lambda: _apply_controlled_v(target_wire=Wires([n_wires - 1]),
                                       control_wire=control_wire)
    u = get_unitary(circ, n_all_wires)

    # Note the sign flip in the following. The sign does not matter when performing the Q unitary
    # because two Vs are used.
    v_ideal = -_make_V(2**n_wires)

    circ = lambda: qml.ControlledQubitUnitary(
        v_ideal, wires=wires, control_wires=control_wire)
    u_ideal = get_unitary(circ, n_all_wires)

    assert np.allclose(u, u_ideal)