Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow multi controlled x decomposition without working wires #5735

Merged
merged 26 commits into from
Jun 6, 2024

Conversation

EmilianoG-byte
Copy link
Contributor

@EmilianoG-byte EmilianoG-byte commented May 23, 2024

Context:
MultiControlled.decomposition() gives an error when no work_wire is provided.

Description of the Change:

Implement decomposition given in Lemma 7.5 of [1] to decompose a general multi controlled unitary gate. MultiControlledX being a particular case.

The function ctrl_decomp_zyz now returns the correct decomposition including a controlled global phase. It now checks that this decomposition only works for single control wire.

A decomposition for single controlled GlobalPhase is now available using Lemma 5.2 of [1], i.e. adds a PhaseShift to the control wire. Raise an exception when used with more than one control.

Benefits:

Decomposition of a MultiControlledX (and in general any multi controlled unitary) is now possible even if work wires are not provided.

If controlled gate is SU(2), this resorts to the specialized algorithm from Lemma 7.9, yielding a decomposition with $\mathcal{O}(\text{len(control wires)})$ gates. For general unitaries, the decomposition yields $\mathcal{O}(\text{len(control wires)}^2)$ gates.

Sources:

[1] Barenco et al. (1995). https://arxiv.org/abs/quant-ph/9503016

[SC-38954]

allow convert_to_su2 to return global phase
Copy link
Contributor

Hello. You may have forgotten to update the changelog!
Please edit doc/releases/changelog-dev.md with:

  • A one-to-two sentence description of the change. You may include a small working example for new features.
  • A link back to this PR.
  • Your name (or GitHub username) in the contributors section.

Copy link

codecov bot commented May 23, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.66%. Comparing base (38b3e74) to head (9cbbec3).
Report is 251 commits behind head on master.

Additional details and impacted files
@@            Coverage Diff             @@
##           master    #5735      +/-   ##
==========================================
- Coverage   99.66%   99.66%   -0.01%     
==========================================
  Files         415      415              
  Lines       39601    39352     -249     
==========================================
- Hits        39469    39219     -250     
- Misses        132      133       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@EmilianoG-byte EmilianoG-byte changed the title Allow multi controlled x decomposition without working wires [WIP] Allow multi controlled x decomposition without working wires May 29, 2024
@EmilianoG-byte EmilianoG-byte marked this pull request as ready for review May 29, 2024 18:40
@EmilianoG-byte
Copy link
Contributor Author

I think the code in line:

if _is_single_qubit_special_unitary(op.base):

should be modified since ctrl_decomp_zyz can now be used for general unitaries. Is this necessary? Currently the code returns the controlled version of each of the operators in the op.base decomposition. In cases like:

base = qml.SX(0)
op = qml.ops.Controlled(base, control_wires=1)
op.decomposition()
>>> [CRZ(1.5707963267948966, wires=<Wires = [1, 0]>),
 CRY(1.5707963267948966, wires=[1, 0])),
 CRZ(-3.141592653589793, wires=<Wires = [1, 0]>),
 ControlledPhaseShift(1.5707963267948966, wires=<Wires = [1, 0]>)]

shouldn't this be further decomposed since we know how to handle the decomposition of each of the controlled operators?

Copy link
Contributor

@mudit2812 mudit2812 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Just one blocking comment about the RuntimeWarning, but otherwise I'm happy to get this in.

pennylane/ops/op_math/controlled.py Show resolved Hide resolved
Co-authored-by: Mudit Pandey <mudit.pandey@xanadu.ai>
Copy link
Contributor

@albi3ro albi3ro left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is going to make a lot of people very happy 🎉

@EmilianoG-byte EmilianoG-byte enabled auto-merge (squash) June 6, 2024 17:32
@EmilianoG-byte EmilianoG-byte merged commit 4abe8ac into master Jun 6, 2024
40 checks passed
@EmilianoG-byte EmilianoG-byte deleted the multicontrol_not_decomposition branch June 6, 2024 20:50
maliasadi added a commit to PennyLaneAI/pennylane-lightning that referenced this pull request Jun 7, 2024
… of control_wires in ctrl_decomp_zyz (#757)

### Before submitting

Please complete the following checklist when submitting a PR:

- [x] 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!

- [x] 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`.

- [x] Add a new entry to the `.github/CHANGELOG.md` file, summarizing
the
      change, and including a link back to the PR.

- [x] 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:**
- [X] Fix the issue with initializing `lightning.kokkos` with zero-qubit
and
- [X] Invalid number of control_wires in ctrl_decomp_zyz (PR
PennyLaneAI/pennylane#5735)
- [x] Add test for `device('lightning.kokkos', wires=0)`

**Description of the Change:**

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**
Fixes #751

---------

Co-authored-by: ringo-but-quantum <github-ringo-but-quantum@xanadu.ai>
multiphaseCFD pushed a commit to PennyLaneAI/pennylane-lightning that referenced this pull request Jun 7, 2024
… of control_wires in ctrl_decomp_zyz (#757)

Please complete the following checklist when submitting a PR:

- [x] 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!

- [x] 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`.

- [x] Add a new entry to the `.github/CHANGELOG.md` file, summarizing
the
      change, and including a link back to the PR.

- [x] 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:**
- [X] Fix the issue with initializing `lightning.kokkos` with zero-qubit
and
- [X] Invalid number of control_wires in ctrl_decomp_zyz (PR
PennyLaneAI/pennylane#5735)
- [x] Add test for `device('lightning.kokkos', wires=0)`

**Description of the Change:**

**Benefits:**

**Possible Drawbacks:**

**Related GitHub Issues:**
Fixes #751

---------

Co-authored-by: ringo-but-quantum <github-ringo-but-quantum@xanadu.ai>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants