Commit 5a0a622
Adding a user-friendly
**Context:**
Users often want to inspect and transform quantum circuits at different
stages of decomposition. This feature will allow them to gain a better
understanding for the decomposition and to think about algorithms at
different layers of abstraction.
Prior to this feature, users only had access to
`qml.devices.preprocess.decompose`- a function focused on device
preprocessing. Over time, it became apparent that this function is
developer focused, making use of hard-coded error messages / exceptions
as well as non-intuitive stopping conditions. This led to some
frustrations where users were trying to explore abstract decompositions
(such as counting the number of CNOTs in a multi-controlled gate). For
now, this function will serve to call the
`qml.devices.preprocess.decompose` function and prune any
developer-focused features. This feature should accomplish the
following,
1. Decomposition of circuits into a desired gate set.
2. Decomposition of circuits into gate sets defined by a
`Callable[Operator, bool]` (e.g. `lambda op: len(op.wires) <= 2`).
3. Decomposition of circuits in stages to understand the process. This
can be done by adjusting the `max_expansion` kwarg.
**Description of the Change:**
The goal of this feature is to create a user-facing function that
enables intuitive decomposition in a less restrictive manner.
Examples:
```python
>>> @partial(decompose, gate_set={qml.Toffoli, "RX", "RZ"})
>>> @qml.qnode(dev)
>>> def circuit():
>>> qml.Hadamard(wires=[0])
>>> qml.Toffoli(wires=[0,1,2])
>>> return qml.expval(qml.Z(0))
>>>
>>> print(qml.draw(circuit)())
0: ──RZ(1.57)──RX(1.57)──RZ(1.57)─╭●─┤ <Z>
1: ───────────────────────────────├●─┤
2: ───────────────────────────────╰X─┤
>>> @partial(decompose, gate_set=lambda op: len(op.wires) <= 2)
>>> @qml.qnode(dev)
>>> def circuit():
>>> qml.Hadamard(wires=[0])
>>> qml.Toffoli(wires=[0,1,2])
>>> return qml.expval(qml.Z(0))
>>>
>>> print(qml.draw(circuit)())
0: ──H────────╭●───────────╭●────╭●──T──╭●─┤ <Z>
1: ────╭●─────│─────╭●─────│───T─╰X──T†─╰X─┤
2: ──H─╰X──T†─╰X──T─╰X──T†─╰X──T──H────────┤
>>> tape = qml.tape.QuantumScript([qml.IsingXX(1.2, wires=(0,1))], [qml.expval(qml.Z(0))])
>>> batch, fn = qml.transforms.decompose(tape, gate_set={"CNOT", "RX", "RZ"})
>>> batch[0].circuit
[CNOT(wires=[0, 1]), RX(1.2, wires=[0]), CNOT(wires=[0, 1]), expval(Z(0))]
```
**Benefits:**
Users can explore quantum circuit decomposition in a more abstract sense
without having to rely on specific device architectures.
**Possible Drawbacks:**
None that I am aware of (yet 😄).
**Related Shortcut Story:**
[sc-51282]qml.transform.decompose function (#6334)1 parent c293405 commit 5a0a622
File tree
5 files changed
+596
-3
lines changed- doc
- introduction
- releases
- pennylane/transforms
- tests/transforms
5 files changed
+596
-3
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
121 | 121 | | |
122 | 122 | | |
123 | 123 | | |
| 124 | + | |
124 | 125 | | |
125 | 126 | | |
126 | 127 | | |
| |||
173 | 174 | | |
174 | 175 | | |
175 | 176 | | |
| 177 | + | |
| 178 | + | |
176 | 179 | | |
177 | 180 | | |
178 | 181 | | |
| |||
208 | 211 | | |
209 | 212 | | |
210 | 213 | | |
211 | | - | |
212 | | - | |
| 214 | + | |
| 215 | + | |
213 | 216 | | |
214 | 217 | | |
215 | 218 | | |
| |||
262 | 265 | | |
263 | 266 | | |
264 | 267 | | |
| 268 | + | |
| 269 | + | |
| 270 | + | |
| 271 | + | |
| 272 | + | |
| 273 | + | |
| 274 | + | |
| 275 | + | |
| 276 | + | |
| 277 | + | |
| 278 | + | |
| 279 | + | |
| 280 | + | |
| 281 | + | |
| 282 | + | |
| 283 | + | |
| 284 | + | |
| 285 | + | |
| 286 | + | |
| 287 | + | |
| 288 | + | |
| 289 | + | |
| 290 | + | |
| 291 | + | |
| 292 | + | |
| 293 | + | |
| 294 | + | |
| 295 | + | |
| 296 | + | |
| 297 | + | |
| 298 | + | |
| 299 | + | |
| 300 | + | |
| 301 | + | |
| 302 | + | |
| 303 | + | |
| 304 | + | |
| 305 | + | |
| 306 | + | |
| 307 | + | |
| 308 | + | |
| 309 | + | |
| 310 | + | |
| 311 | + | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
| 316 | + | |
| 317 | + | |
| 318 | + | |
| 319 | + | |
| 320 | + | |
| 321 | + | |
| 322 | + | |
| 323 | + | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
265 | 377 | | |
266 | 378 | | |
267 | 379 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
4 | 4 | | |
5 | 5 | | |
6 | 6 | | |
| 7 | + | |
| 8 | + | |
| 9 | + | |
7 | 10 | | |
8 | 11 | | |
9 | 12 | | |
| |||
329 | 332 | | |
330 | 333 | | |
331 | 334 | | |
332 | | - | |
| 335 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
| 54 | + | |
54 | 55 | | |
55 | 56 | | |
56 | 57 | | |
| |||
361 | 362 | | |
362 | 363 | | |
363 | 364 | | |
| 365 | + | |
0 commit comments