|
| 1 | +import ast |
| 2 | + |
| 3 | +from psiqworkbench import QPU, QUInt, QFixed, Qubrick |
| 4 | +from psiqworkbench.filter_presets import BIT_DEFAULT |
| 5 | + |
| 6 | +from qmath.utils.symbolic import alloc_temp_qreg_like |
| 7 | +from qmath.func.common import MultiplyAdd, MultiplyConstAdd, Add, AddConst, Negate |
| 8 | +from qmath.func.square import Square |
| 9 | + |
| 10 | +from qmath.utils.gates import ParallelCnot |
| 11 | + |
| 12 | +# Type alias to represent quantum register or a literal number. |
| 13 | +QValue = QFixed | float |
| 14 | + |
| 15 | + |
| 16 | +# Ensures that x is of type QValue. |
| 17 | +def _make_qvalue(x) -> QValue: |
| 18 | + if isinstance(x, QFixed): |
| 19 | + return x |
| 20 | + if isinstance(x, int) or isinstance(x, float): |
| 21 | + return float(x) |
| 22 | + raise ValueError("Unsupported type", type(x)) |
| 23 | + |
| 24 | + |
| 25 | +ops = [] |
| 26 | + |
| 27 | + |
| 28 | +class EvaluateExpression(Qubrick): |
| 29 | + """Evaluates arithmetic expression.""" |
| 30 | + |
| 31 | + def __init__(self, expr: str, mutable_vars: set[str] = None, **kwargs): |
| 32 | + super().__init__(**kwargs) |
| 33 | + self.expr = expr |
| 34 | + self.vars = dict() |
| 35 | + self.immutable_regs = set() |
| 36 | + self.mutable_vars = mutable_vars or set() |
| 37 | + |
| 38 | + def _make_copy(self, x: QFixed) -> QFixed: |
| 39 | + _, ans = alloc_temp_qreg_like(self, x) |
| 40 | + ParallelCnot().compute(x, ans) |
| 41 | + return ans |
| 42 | + |
| 43 | + def _implement_unary_op(self, op: ast.BinOp, arg: QValue) -> QValue: |
| 44 | + if isinstance(op, ast.USub): |
| 45 | + return self._negate(arg) |
| 46 | + raise ValueError(f"Unsupported unary op: {op}.") |
| 47 | + |
| 48 | + def _implement_binary_op(self, op: ast.BinOp, arg1: QValue, arg2: QValue) -> QValue: |
| 49 | + if isinstance(op, ast.Add): |
| 50 | + return self._add(arg1, arg2) |
| 51 | + if isinstance(op, ast.Sub): |
| 52 | + return self._sub(arg1, arg2) |
| 53 | + if isinstance(op, ast.Mult): |
| 54 | + return self._mul(arg1, arg2) |
| 55 | + raise ValueError(f"Unsupported binary op: {op}.") |
| 56 | + |
| 57 | + def _negate(self, arg: QValue) -> QValue: |
| 58 | + if isinstance(arg, float): |
| 59 | + return -arg |
| 60 | + assert isinstance(arg, QFixed) |
| 61 | + if arg.mask() in self.immutable_regs: |
| 62 | + return self._negate(self._make_copy(arg)) |
| 63 | + Negate().compute(arg) |
| 64 | + return arg |
| 65 | + |
| 66 | + def _add(self, arg1: QValue, arg2: QValue) -> QValue: |
| 67 | + if isinstance(arg1, float) and isinstance(arg2, float): |
| 68 | + return arg1 + arg2 |
| 69 | + if isinstance(arg1, float): |
| 70 | + return self._add(arg2, arg1) |
| 71 | + |
| 72 | + assert isinstance(arg1, QFixed) |
| 73 | + |
| 74 | + if isinstance(arg2, QFixed): |
| 75 | + # Quantum-quantum addition. |
| 76 | + if arg1.mask() in self.immutable_regs and arg2.mask() in self.immutable_regs: |
| 77 | + return self._add(self._make_copy(arg1), arg2) |
| 78 | + if arg1.mask() in self.immutable_regs: |
| 79 | + return self._add(arg2, arg1) |
| 80 | + Add().compute(arg1, arg2) |
| 81 | + return arg1 |
| 82 | + else: |
| 83 | + assert isinstance(arg2, float) |
| 84 | + if arg1.mask() in self.immutable_regs: |
| 85 | + return self._add(self._make_copy(arg1), arg2) |
| 86 | + AddConst(arg2).compute(arg1) |
| 87 | + return arg1 |
| 88 | + |
| 89 | + def _sub(self, arg1: QValue, arg2: QValue) -> QValue: |
| 90 | + if isinstance(arg1, float): |
| 91 | + return self._add(-arg1, arg2) |
| 92 | + if isinstance(arg2, float): |
| 93 | + return self._add(arg1, -arg2) |
| 94 | + |
| 95 | + assert isinstance(arg1, QFixed) |
| 96 | + assert isinstance(arg2, QFixed) |
| 97 | + |
| 98 | + if arg1.mask() not in self.immutable_regs: |
| 99 | + # arg1 -= arg2 |
| 100 | + with Negate().computed(arg1): |
| 101 | + Add().compute(arg1, arg2) |
| 102 | + return arg1 |
| 103 | + elif arg2.mask() not in self.immutable_regs: |
| 104 | + # arg2 := -arg2 |
| 105 | + # arg2 += arg1 |
| 106 | + Negate().compute(arg2) |
| 107 | + Add().compute(arg2, arg1) |
| 108 | + return arg2 |
| 109 | + else: |
| 110 | + # Both immutable. Allocate answer. |
| 111 | + return self._negate(arg1, self._make_copy(arg2)) |
| 112 | + |
| 113 | + def _mul(self, arg1: QValue, arg2: QValue) -> QValue: |
| 114 | + if isinstance(arg1, float) and isinstance(arg2, float): |
| 115 | + return arg1 * arg2 |
| 116 | + if isinstance(arg1, float): |
| 117 | + return self._mul(arg2, arg1) |
| 118 | + |
| 119 | + assert isinstance(arg1, QFixed) |
| 120 | + _, ans = alloc_temp_qreg_like(self, arg1) |
| 121 | + |
| 122 | + if isinstance(arg2, QFixed): |
| 123 | + if arg1.mask() == arg2.mask(): |
| 124 | + Square().compute(arg1, ans) |
| 125 | + return ans |
| 126 | + MultiplyAdd().compute(ans, arg1, arg2) |
| 127 | + else: |
| 128 | + assert isinstance(arg2, float) |
| 129 | + MultiplyConstAdd(arg2).compute(ans, arg1) |
| 130 | + return ans |
| 131 | + |
| 132 | + def _convert_ast_node(self, node) -> QFixed | float: |
| 133 | + if isinstance(node, ast.BinOp): |
| 134 | + arg1 = self._convert_ast_node(node.left) |
| 135 | + arg2 = self._convert_ast_node(node.right) |
| 136 | + return self._implement_binary_op(node.op, arg1, arg2) |
| 137 | + elif isinstance(node, ast.UnaryOp): |
| 138 | + arg = self._convert_ast_node(node.operand) |
| 139 | + return self._implement_unary_op(node.op, arg) |
| 140 | + elif isinstance(node, ast.Name): |
| 141 | + assert node.id in self.vars |
| 142 | + return self.vars[node.id] |
| 143 | + elif isinstance(node, ast.Constant): |
| 144 | + return _make_qvalue(node.value) |
| 145 | + else: |
| 146 | + raise ValueError(f"Cannot handle: {node}") |
| 147 | + |
| 148 | + def _compute(self, args: dict): |
| 149 | + self.vars = dict() |
| 150 | + for key, value in args.items(): |
| 151 | + value = _make_qvalue(value) |
| 152 | + self.vars[key] = value |
| 153 | + if key not in self.mutable_vars and isinstance(value, QFixed): |
| 154 | + self.immutable_regs.add(value.mask()) |
| 155 | + |
| 156 | + root = ast.parse(self.expr, mode="eval") |
| 157 | + ans = self._convert_ast_node(root.body) |
| 158 | + self.set_result_qreg(ans) |
0 commit comments