|
| 1 | +# Copyright 2022-2023 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 | +"""Tests for operator in Catalyst.""" |
| 15 | + |
| 16 | +# pylint: disable = useless-parent-delegation, missing-function-docstring, missing-class-docstring |
| 17 | + |
| 18 | +# RUN: %PYTHON %s | FileCheck %s |
| 19 | + |
| 20 | +import numpy as np |
| 21 | +import pennylane as qp |
| 22 | + |
| 23 | + |
| 24 | +class NoParams(qp.core.Operator2): |
| 25 | + |
| 26 | + def __init__(self, wires): |
| 27 | + super().__init__(wires=wires) |
| 28 | + |
| 29 | + |
| 30 | +@qp.qjit(target="mlir", capture=True) |
| 31 | +@qp.qnode(qp.device("null.qubit", wires=2)) |
| 32 | +def c_no_params(): |
| 33 | + # CHECK: [[q0:%.+]] = qref.get {{%.+}} |
| 34 | + # CHECK: qref.operator "NoParams"() qubits([[q0]]) |
| 35 | + # CHECK: static_data = {} |
| 36 | + # CHECK: param_map = {} qubit_map = {wires = [0]} |
| 37 | + NoParams(wires=0) |
| 38 | + |
| 39 | + # CHECK: [[q1:%.+]] = qref.get {{%.+}} |
| 40 | + # CHECK: [[q2:%.+]] = qref.get {{%.+}} |
| 41 | + # CHECK: qref.operator "NoParams"() qubits([[q1]], [[q2]]) |
| 42 | + # CHECK: static_data = {} |
| 43 | + # CHECK: param_map = {} qubit_map = {wires = [0, 1]} |
| 44 | + NoParams(wires=(0, 1)) |
| 45 | + return qp.state() |
| 46 | + |
| 47 | + |
| 48 | +print(c_no_params.mlir) |
| 49 | + |
| 50 | + |
| 51 | +class SingleParam(qp.core.Operator2): |
| 52 | + |
| 53 | + dynamic_argnames = ("x",) |
| 54 | + |
| 55 | + def __init__(self, x, wires): |
| 56 | + super().__init__(x, wires=wires) |
| 57 | + |
| 58 | + |
| 59 | +@qp.qjit(target="mlir", capture=True) |
| 60 | +@qp.qnode(qp.device("null.qubit", wires=3)) |
| 61 | +def c_single_param(x: float): |
| 62 | + |
| 63 | + # CHECK: [[q0:%.+]] = qref.get {{%.+}} |
| 64 | + |
| 65 | + # CHECK: qref.operator "SingleParam"({{%.+}}: tensor<f64>) qubits([[q0]]) |
| 66 | + # CHECK: static_data = {} |
| 67 | + # CHECK: param_map = {x = [0]} qubit_map = {wires = [0]} |
| 68 | + SingleParam(x, 0) |
| 69 | + |
| 70 | + # CHECK: [[q1:%.+]] = qref.get {{%.+}} |
| 71 | + # CHECK: [[q2:%.+]] = qref.get {{%.+}} |
| 72 | + # CHECK: qref.operator "SingleParam"({{%.+}}: tensor<4x4xf64>) qubits([[q1]], [[q2]]) |
| 73 | + # CHECK: static_data = {} |
| 74 | + # CHECK: param_map = {x = [0]} qubit_map = {wires = [0, 1]} |
| 75 | + SingleParam(np.eye(4), (1, 2)) |
| 76 | + |
| 77 | + return qp.state() |
| 78 | + |
| 79 | + |
| 80 | +print(c_single_param.mlir) |
| 81 | + |
| 82 | + |
| 83 | +class CompilableData(qp.core.Operator2): |
| 84 | + |
| 85 | + compilable_argnames = ("a", "b", "thing") |
| 86 | + |
| 87 | + def __init__(self, a, b, thing, wires): |
| 88 | + super().__init__(a=a, b=b, thing=thing, wires=wires) |
| 89 | + |
| 90 | + |
| 91 | +@qp.qjit(capture=True, target="mlir") |
| 92 | +@qp.qnode(qp.device("null.qubit", wires=3)) |
| 93 | +def c_compilable(): |
| 94 | + # CHECK: [[q1:%.+]] = qref.get {{%.+}} |
| 95 | + # CHECK: [[q2:%.+]] = qref.get {{%.+}} |
| 96 | + # CHECK: qref.operator "CompilableData"() qubits([[q1]], [[q2]]) |
| 97 | + # CHECK: static_data = {a = true, b = "some string", thing = [1, true, "string"]} |
| 98 | + # CHECK: param_map = {} qubit_map = {wires = [0, 1]} |
| 99 | + |
| 100 | + CompilableData(True, "some string", (1, True, "string"), wires=(0, 1)) |
| 101 | + |
| 102 | + return qp.state() |
| 103 | + |
| 104 | + |
| 105 | +print(c_compilable.mlir) |
| 106 | + |
| 107 | + |
| 108 | +class MultipleRegisters(qp.core.Operator2): |
| 109 | + |
| 110 | + wire_argnames = ("reg1", "reg2") |
| 111 | + |
| 112 | + def __init__(self, reg1, reg2): |
| 113 | + super().__init__(reg1=reg1, reg2=reg2) |
| 114 | + |
| 115 | + |
| 116 | +@qp.qjit(capture=True, target="mlir") |
| 117 | +@qp.qnode(qp.device("null.qubit", wires=5)) |
| 118 | +def c_multiple_registers(): |
| 119 | + # CHECK: [[q0:%.+]] = qref.get {{%.+}} |
| 120 | + # CHECK: [[q2:%.+]] = qref.get {{%.+}} |
| 121 | + # CHECK: [[q3:%.+]] = qref.get {{%.+}} |
| 122 | + # CHECK: [[q4:%.+]] = qref.get {{%.+}} |
| 123 | + |
| 124 | + # CHECK: qref.operator "MultipleRegisters"() qubits([[q0]], [[q2]], [[q3]], [[q4]]) |
| 125 | + # CHECK: static_data = {} |
| 126 | + # CHECK: param_map = {} qubit_map = {reg1 = [0], reg2 = [1, 2, 3]} |
| 127 | + MultipleRegisters(0, (2, 3, 4)) |
| 128 | + return qp.state() |
| 129 | + |
| 130 | + |
| 131 | +print(c_multiple_registers.mlir) |
| 132 | + |
| 133 | + |
| 134 | +class MultiParams(qp.core.Operator2): |
| 135 | + |
| 136 | + dynamic_argnames = ("a", "b", "c") |
| 137 | + |
| 138 | + # note also having non-standard order with dynamic inputs after wires |
| 139 | + def __init__(self, wires, a, b, c): |
| 140 | + super().__init__(wires, a, b, c) |
| 141 | + |
| 142 | + |
| 143 | +@qp.qjit(capture=True, target="mlir") |
| 144 | +@qp.qnode(qp.device("null.qubit", wires=1)) |
| 145 | +def c_multi_params(): |
| 146 | + # CHECK: [[q0:%.+]] = qref.get {{%.+}} |
| 147 | + |
| 148 | + # pylint: disable=line-too-long |
| 149 | + # CHECK: qref.operator "MultiParams"({{%.+}}: tensor<f64>, {{%.+}}: tensor<4x2x1xf64>, {{%.+}}: tensor<3xi64>) qubits([[q0]]) |
| 150 | + # CHECK: static_data = {} |
| 151 | + # CHECK: param_map = {a = [0], b = [1], c = [2]} qubit_map = {wires = [0]} |
| 152 | + MultiParams(0, 0.5, c=np.array([1, 2, 3]), b=np.zeros((4, 2, 1))) |
| 153 | + return qp.state() |
| 154 | + |
| 155 | + |
| 156 | +print(c_multi_params.mlir) |
0 commit comments