You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Use native implementation for adjoints in (control) operations (#1063)
### Before submitting
Please complete the following checklist when submitting a PR:
- [ ] All new features must include a unit test.
If you've fixed a bug or added code that should be tested, add a test to
the
[`tests`](../tests) directory!
- [ ] All new functions and code must be clearly commented and
documented.
If you do make documentation changes, make sure that the docs build and
render correctly by running `make docs`.
- [ ] Ensure that the test suite passes, by running `make test`.
- [ ] Add a new entry to the `.github/CHANGELOG.md` file, summarizing
the
change, and including a link back to the PR.
- [ ] Ensure that code is properly formatted by running `make format`.
When all the above are checked, delete everything above the dashed
line and fill in the pull request template.
------------------------------------------------------------------------------------------------------------
**Context:**
Currently in `_apply_lightning`, we check for whether an operation is
`Adjoint`, then we apply the operation with an adjoint (`inv_param`)
flag. However, in cases where we have:
- adjoint(s) within control - e.g. `control(adjoint(gate))`
- control within adjoint - e.g. `adjoint(control(gate))`,
these are all applied as matrices.
**Description of the Change:**
`_apply_lightning` and `_apply_lightning_controlled` checks for adjoint
in an operation, and if it's an adjoint it applies the base operation
with an adjoint flag, instead of treating everything as a matrix.
So in effect we have:
`control(adjoint(gate))` -> `control(gate with adjoint)`
`adjoint(control(gate))` -> `control(gate with adjoint)`
which are implemented natively in C++ (if the `gate` is supported),
yielding better performance
**Benefits:**
adjoint(ctrl()) will see the most speedup, especially with large number
of control wires, since we use native control operation which contains
less wires than the equivalent matrix, and needs to be operated on less
wires. adjoint(ctrl()) will see some speed-up, since we are now able to
use the native named gate implementation in C++.
Example timing improvement:
4 ctrl wires
LQ:
| LQ, 25 qubits, 500 repeats | master | branch |
|-------------------------------------|--------|-------|
| ctrl(adjoint(IsingXX)) | 9.6s | 6.0s |
| ctrl(adjoint(DoubleExcitationPlus)) | 27.6s | 9.2s |
| LQ, 25 qubits, 100 repeats | master | branch |
|-------------------------------------|------------------|--------|
| adjoint(ctrl(IsingXX)) | 267s | 2.9s|
| adjoint(ctrl(DoubleExcitationPlus)) | 1002s| 3.6s |
Baseline:
| LQ, 25 qubits, 500 repeats | master | branch |
|-------------------------------------|--------|--------|
| ctrl(IsingXX) | 6.1s |6.1s |
| ctrl(DoubleExcitationPlus)| 9.1s | 9.1s |
LG:
| LG, 31 qubits, 1000 repeats | master | branch |
|-------------------------------------|--------|--------|
| ctrl(adjoint(IsingXX)) | 4.9s | 4.8s |
| ctrl(adjoint(DoubleExcitationPlus)) | 5.0s | 4.9s |
| LG, 31 qubits, 1000 repeats | master | branch |
|-------------------------------------|-------------|--------|
| adjoint(ctrl(IsingXX)) | 119s | 4.8s|
| adjoint(ctrl(DoubleExcitationPlus)) | 208s | 4.9s |
Baseline:
| LG, 31 qubits, 1000 repeats | master | branch |
|-------------------------------------|-------------------|--------|
| ctrl(IsingXX) | 4.8s | 4.8s |
| ctrl(DoubleExcitationPlus)| 4.9s | 4.9s |
LK:
| LK, 25 qubits, 500 repeats | master | branch |
|-------------------------------------|--------|--------|
| ctrl(adjoint(IsingXX)) | 8.5s |5.7s |
| ctrl(adjoint(DoubleExcitationPlus)) | 24.5s | 7.6s |
| LK, 25 qubits, 100 repeats | master | branch |
|-------------------------------------|-----|--------|
| adjoint(ctrl(IsingXX)) | 235s | 2.6s |
| adjoint(ctrl(DoubleExcitationPlus)) | 867s | 2.9s |
Baseline:
| LK, 25 qubits, 500 repeats | master |branch |
|-------------------------------------|-------------|--------|
| ctrl(IsingXX) | 5.6s |5.8s |
| ctrl(DoubleExcitationPlus)| 7.7s | 7.6 s |
**Possible Drawbacks:**
**Related GitHub Issues:**
[sc-79430]
---------
Co-authored-by: ringo-but-quantum <[email protected]>
Co-authored-by: Christina Lee <[email protected]>
Co-authored-by: Amintor Dusko <[email protected]>
0 commit comments