def test_Z(): """Test for the _make_Z function""" dim = 4 Z_expected = -np.eye(dim) Z_expected[0, 0] = 1 Z = _make_Z(dim) assert np.allclose(Z, Z_expected)
def test_apply_controlled_z(n_wires): """Test if the _apply_controlled_z performs the correct transformation by reconstructing the unitary and comparing against the one provided in _make_Z.""" n_all_wires = n_wires + 1 wires = Wires(range(n_wires)) control_wire = n_wires work_wires = None circ = lambda: _apply_controlled_z( wires=wires, control_wire=control_wire, work_wires=work_wires) 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 Zs are used. z_ideal = -_make_Z(2**n_wires) circ = lambda: qml.ControlledQubitUnitary( z_ideal, wires=wires, control_wires=control_wire) u_ideal = get_unitary(circ, n_all_wires) assert np.allclose(u, u_ideal)