|
21 | 21 | import jax.numpy as jnp |
22 | 22 | import numpy as np |
23 | 23 | import pennylane as qp |
| 24 | +from jax._src.interpreters.mlir import dtype_to_ir_type |
24 | 25 | from jax._src.lib.mlir import ir |
25 | 26 | from jax.core import ShapedArray |
26 | 27 | from pennylane.pytrees import flatten, unflatten |
27 | 28 |
|
| 29 | +from catalyst.jax_extras.lowering import mlir_build_context |
| 30 | + |
28 | 31 | _MLIR_DTYPES_TO_PY_DTYPES = { |
29 | 32 | "i1": jnp.bool_, |
30 | 33 | "i8": jnp.int8, |
|
38 | 41 | "complex<f64>": jnp.complex128, |
39 | 42 | } |
40 | 43 |
|
41 | | -_PY_DTYPES_TO_MLIR_DTYPES = {v: k for k, v in _MLIR_DTYPES_TO_PY_DTYPES.items()} | { |
42 | | - float: "f64", |
43 | | - int: "i64", |
44 | | - complex: "complex<f64>", |
45 | | - ir.F16Type: "f16", |
46 | | - ir.F32Type: "f32", |
47 | | - ir.F64Type: "f64", |
48 | | - (ir.ComplexType, ir.F32Type): "complex<f32>", |
49 | | - (ir.ComplexType, ir.F64Type): "complex<f64>", |
50 | | - np.dtype("bool_"): "i1", |
51 | | - np.dtype("int8"): "i8", |
52 | | - np.dtype("int16"): "i16", |
53 | | - np.dtype("int32"): "i32", |
54 | | - np.dtype("int64"): "i64", |
55 | | - np.dtype("float16"): "f16", |
56 | | - np.dtype("float32"): "f32", |
57 | | - np.dtype("float64"): "f64", |
58 | | - np.dtype("complex64"): "complex<f32>", |
59 | | - np.dtype("complex128"): "complex<f64>", |
60 | | -} |
61 | | - |
62 | 44 |
|
63 | 45 | def convert_item_to_mlir_type(item, is_special_lowering=False): |
64 | | - """Convert a string or PennyLane AbstractArray to an mlir type annotation.""" |
| 46 | + """Convert a string or PennyLane AbstractArray to an mlir type annotation. |
| 47 | +
|
| 48 | + The type is spelled by MLIR's own printer, applied to the type the value lowers to, which is |
| 49 | + what ``printDynamicShape`` in mlir/lib/Quantum/IR/QuantumInterfaces.cpp does as well. One |
| 50 | + printer spelling both sides is what keeps a rule compiled here findable by the |
| 51 | + ``graph-decomposition`` pass. |
| 52 | + """ |
65 | 53 | if isinstance(item, str): |
66 | 54 | return item |
67 | 55 |
|
68 | | - if item.shape == (): |
69 | | - if is_special_lowering: |
70 | | - return _PY_DTYPES_TO_MLIR_DTYPES[item.dtype] |
71 | | - return "tensor<" + _PY_DTYPES_TO_MLIR_DTYPES[item.dtype] + ">" |
72 | | - |
73 | | - return ( |
74 | | - "tensor<" |
75 | | - + "x".join(str(dim_size) for dim_size in item.shape) |
76 | | - + "x" |
77 | | - + _PY_DTYPES_TO_MLIR_DTYPES[item.dtype] |
78 | | - + ">" |
79 | | - ) |
| 56 | + with mlir_build_context(): |
| 57 | + element_type = dtype_to_ir_type(np.dtype(item.dtype)) |
| 58 | + if is_special_lowering and item.shape == (): |
| 59 | + return str(element_type) |
| 60 | + return str(ir.RankedTensorType.get(item.shape, element_type)) |
80 | 61 |
|
81 | 62 |
|
82 | 63 | def get_dummy_values_for_arg(arg): |
|
0 commit comments