|
| 1 | +# Copyright 2025 Xanadu Quantum Technologies Inc. |
| 2 | + |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | + |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | + |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +"""Unit tests for wrappers for capturing catalyst passes""" |
| 15 | + |
| 16 | +import pytest |
| 17 | + |
| 18 | +pytest.importorskip("catalyst") |
| 19 | +# pylint: disable=wrong-import-position |
| 20 | + |
| 21 | +import pennylane as qml |
| 22 | +from pennylane.capture import make_plxpr |
| 23 | +from pennylane.ftqc.catalyst_pass_aliases import ( |
| 24 | + commute_ppr, |
| 25 | + merge_ppr_ppm, |
| 26 | + ppm_to_mbqc, |
| 27 | + reduce_t_depth, |
| 28 | + to_ppr, |
| 29 | +) |
| 30 | + |
| 31 | +pytestmark = pytest.mark.external |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.catalyst |
| 35 | +@pytest.mark.usefixtures("enable_disable_plxpr") |
| 36 | +@pytest.mark.parametrize( |
| 37 | + "pass_fn", [to_ppr, commute_ppr, merge_ppr_ppm, ppm_to_mbqc, reduce_t_depth] |
| 38 | +) |
| 39 | +def test_pass_is_captured(pass_fn): |
| 40 | + |
| 41 | + @pass_fn |
| 42 | + @qml.qnode(qml.device("lightning.qubit", wires=3), shots=1000) |
| 43 | + def circ(): |
| 44 | + qml.H(0) |
| 45 | + qml.S(0) |
| 46 | + qml.T(1) |
| 47 | + qml.CNOT([0, 1]) |
| 48 | + return qml.sample() |
| 49 | + |
| 50 | + plxpr = make_plxpr(circ)() |
| 51 | + prim = plxpr.eqns[0].primitive |
| 52 | + assert prim.name == pass_fn.__name__ + "_transform" |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.catalyst |
| 56 | +@pytest.mark.usefixtures("enable_disable_plxpr") |
| 57 | +@pytest.mark.parametrize( |
| 58 | + "pass_fn, pass_name", |
| 59 | + [ |
| 60 | + (to_ppr, "to-ppr"), |
| 61 | + (commute_ppr, "commute-ppr"), |
| 62 | + (merge_ppr_ppm, "merge-ppr-ppm"), |
| 63 | + (ppm_to_mbqc, "ppm-to-mbqc"), |
| 64 | + (reduce_t_depth, "reduce-t-depth"), |
| 65 | + ], |
| 66 | +) |
| 67 | +def test_converstion_to_mlir(pass_fn, pass_name): |
| 68 | + """Test that we can generate MLIR from the captured circuit and that the generated MLIR |
| 69 | + includes the pass name we are mapping to""" |
| 70 | + |
| 71 | + @qml.qjit(target="mlir") |
| 72 | + @pass_fn |
| 73 | + @qml.qnode(qml.device("lightning.qubit", wires=3), shots=1000) |
| 74 | + def circ(): |
| 75 | + qml.H(0) |
| 76 | + qml.S(0) |
| 77 | + qml.T(1) |
| 78 | + qml.CNOT([0, 1]) |
| 79 | + return qml.sample() |
| 80 | + |
| 81 | + assert pass_name in circ.mlir |
| 82 | + |
| 83 | + |
| 84 | +@pytest.mark.catalyst |
| 85 | +@pytest.mark.parametrize( |
| 86 | + "pass_fn", [to_ppr, commute_ppr, merge_ppr_ppm, ppm_to_mbqc, reduce_t_depth] |
| 87 | +) |
| 88 | +def test_pass_without_qjit_raises_error(pass_fn): |
| 89 | + """Test that trying to apply the transform without QJIT raises an error""" |
| 90 | + |
| 91 | + @pass_fn |
| 92 | + @qml.qnode(qml.device("lightning.qubit", wires=3), shots=1000) |
| 93 | + def circ(): |
| 94 | + qml.H(0) |
| 95 | + qml.S(0) |
| 96 | + qml.T(1) |
| 97 | + qml.CNOT([0, 1]) |
| 98 | + return qml.sample() |
| 99 | + |
| 100 | + with pytest.raises(NotImplementedError, match="only implemented when using capture and QJIT"): |
| 101 | + circ() |
0 commit comments