Skip to content

Commit

Permalink
Adding missing QJIT tests for measurement processes (#1209)
Browse files Browse the repository at this point in the history
Adding tests marked with `xfail` for measurement processes not supported
by Catalyst. This PR addresses [sc-72627].
  • Loading branch information
willjmax authored Nov 5, 2024
1 parent f984487 commit b2e489b
Show file tree
Hide file tree
Showing 2 changed files with 169 additions and 0 deletions.
124 changes: 124 additions & 0 deletions frontend/test/pytest/test_measurements_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

from catalyst import qjit

# pylint: disable=too-many-lines


class TestSample:
"""Test sample."""
Expand Down Expand Up @@ -138,6 +140,25 @@ def counts_2qbit(x: float, y: float):
observed = counts_2qbit(0, np.pi)
assert np.array_equal(observed, expected)

@pytest.mark.xfail(reason="Not supported by Catalyst")
def test_counts_all_outcomes(self, backend):
"""Test counts with all_outcomes=True."""

@qjit
@qml.qnode(qml.device(backend, wires=2, shots=1000))
def counts_2qbit(x: float):
qml.RX(x, wires=0)
qml.RY(x, wires=1)
return qml.counts(all_outcomes=True)

expected = {"00": 1000, "01": 0, "10": 0, "11": 0}
observed = counts_2qbit(0.0)
assert np.array_equal(observed, expected)

expected = {"00": 0, "01": 0, "10": 0, "11": 1000}
observed = counts_2qbit(np.pi)
assert np.array_equal(observed, expected)


class TestExpval:
def test_named(self, backend):
Expand Down Expand Up @@ -994,5 +1015,108 @@ def circuit():
return qml.density_matrix([0])


class TestVnEntropy:
"""Test vnentropy."""

@pytest.mark.xfail(reason="Not supported on lightning.")
def test_vn_entropy(self):
"""Test that VnEntropy can be used with Catalyst."""

dev = qml.device("lightning.qubit", wires=2)

@qjit
@qml.qnode(dev)
def circuit_entropy(x):
qml.IsingXX(x, wires=[0, 1])
return qml.vn_entropy(wires=[0])

expected = 0.6931471805599453
assert circuit_entropy(np.pi / 2) == expected


class TestMutualInfo:
"""Test mutualinfo."""

@pytest.mark.xfail(reason="Not supported on lightning.")
def test_mutual_info(self):
"""Test that MutualInfo can be used with Catalyst."""

dev = qml.device("lightning.qubit", wires=range(2))

@qjit
@qml.qnode(dev)
def mutual_info_circuit():
qml.Hadamard(0)
qml.CNOT((0, 1))
qml.RX(0, wires=0)
return qml.mutual_info(0, 1)

expected = 1.3862943611198906
assert mutual_info_circuit() == expected


class TestShadow:
"""Test shadow."""

@pytest.mark.xfail(reason="Not supported on lightning.")
def test_shadow(self):
"""Test that Shadow can be used with Catalyst."""

dev = qml.device("lightning.qubit", wires=range(2))

@qjit
@qml.qnode(dev)
def classical_shadow_circuit():
qml.Hadamard(0)
qml.CNOT(wires=[0, 1])
return qml.classical_shadow(wires=[0, 1])

expected_bits = [[1, 1], [0, 1]]
expected_recipes = [[0, 1], [0, 2]]
actual_bits, actual_recipes = classical_shadow_circuit()
assert expected_bits == actual_bits
assert expected_recipes == actual_recipes


class TestShadowExpval:
"""Test shadowexpval."""

@pytest.mark.xfail(reason="Not supported on lightning.")
def test_shadow_expval(self):
"""Test that ShadowExpVal can be used with Catalyst."""

dev = qml.device("lightning.qubit", wires=range(2))

@qjit
@qml.qnode(dev)
def shadow_expval_circuit(x, obs):
qml.Hadamard(0)
qml.CNOT((0, 1))
qml.RX(x, wires=0)
return qml.shadow_expval(obs)

H = qml.Hamiltonian([1.0, 1.0], [qml.Z(0) @ qml.Z(1), qml.X(0) @ qml.X(1)])
expected = 1.917
assert shadow_expval_circuit(0, H) == expected


class TestPurity:
"""Test purity."""

@pytest.mark.xfail(reason="Not supported on lightning.")
def test_purity(self):
"""Test that Purity can be used with Catalyst."""

dev = qml.device("lightning.qubit", wires=[0])

@qjit
@qml.qnode(dev)
def purity_circuit():
return qml.purity(wires=[0])

expected = 1.0
assert purity_circuit() == expected


if __name__ == "__main__":
pytest.main(["-x", __file__])
45 changes: 45 additions & 0 deletions frontend/test/pytest/test_measurements_shots_results.py
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,51 @@ def circuit(theta):
assert np.allclose(result, expected, atol=tol_stochastic, rtol=tol_stochastic)


class TestShadow:
"""Test shadow."""

@pytest.mark.xfail(reason="Not supported on lightning.")
def test_shadow(self):
"""Test that Shadow can be used with Catalyst."""

dev = qml.device("lightning.qubit", wires=range(2), shots=10000)

@qjit
@qml.qnode(dev)
def classical_shadow_circuit():
qml.Hadamard(0)
qml.CNOT(wires=[0, 1])
return qml.classical_shadow(wires=[0, 1])

expected_bits = [[1, 1], [0, 1]]
expected_recipes = [[0, 1], [0, 2]]
actual_bits, actual_recipes = classical_shadow_circuit()
assert expected_bits == actual_bits
assert expected_recipes == actual_recipes


class TestShadowExpval:
"""Test shadowexpval."""

@pytest.mark.xfail(reason="TypeError in Catalyst")
def test_shadow_expval(self):
"""Test that ShadowExpVal can be used with Catalyst."""

dev = qml.device("lightning.qubit", wires=range(2), shots=10000)

@qjit
@qml.qnode(dev)
def shadow_expval_circuit(x, obs):
qml.Hadamard(0)
qml.CNOT((0, 1))
qml.RX(x, wires=0)
return qml.shadow_expval(obs)

H = qml.Hamiltonian([1.0, 1.0], [qml.Z(0) @ qml.Z(1), qml.X(0) @ qml.X(1)])
expected = 1.9917
assert shadow_expval_circuit(0, H) == expected


class TestOtherMeasurements:
"""Test other measurement processes."""

Expand Down

0 comments on commit b2e489b

Please sign in to comment.