Navigation Menu

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

undo_swaps #1960

Merged
merged 19 commits into from Dec 2, 2021
Merged

undo_swaps #1960

merged 19 commits into from Dec 2, 2021

Conversation

KetpuntoG
Copy link
Contributor

Context: The Permute operation uses many SWAP gates to be implemented. In (#1324) a study was proposed to make this operator native to default_qubit in order to make the circuit more efficient. Instead, I have created a transformation that removes all the SWAP gates from the circuit by playing with the wires of the operations. I think it is a more efficient and flexible solution.

Description of the Change:
With a single reading of the circuit from right to left, all SWAPS in the circuit can be eliminated.

Example:

Without undo_swaps:

import pennylane as qml
dev = qml.device('default.qubit', wires=3)

@qml.qnode(dev)
def qfunc():
    qml.Hadamard(wires=0)
    qml.PauliX(wires=1)
    qml.SWAP(wires=[0,1])
    qml.SWAP(wires=[0,2])
    qml.PauliY(wires=0)
    return qml.expval(qml.PauliZ(0))

print(qml.draw(qfunc)())

0: ──H──╭SWAP──╭SWAP──Y──┤ ⟨Z⟩
1: ──X──╰SWAP──│────────┤
2: ──────────╰SWAP─────┤

With undo_swaps:

@qml.qnode(dev)
@qml.transforms.undo_swaps
def qfunc():
    qml.Hadamard(wires=0)
    qml.PauliX(wires=1)
    qml.SWAP(wires=[0,1])
    qml.SWAP(wires=[0,2])
    qml.PauliY(wires=0)
    return qml.expval(qml.PauliZ(0))

print(qml.draw(qfunc)())

0: ──Y──┤ ⟨Z⟩
1: ──H──┤
2: ──X──┤

Benefits:
The computational cost of the circuit calculation is reduced.

Related GitHub Issues:
(#1324)

@github-actions
Copy link
Contributor

github-actions bot commented Dec 1, 2021

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.

@codecov
Copy link

codecov bot commented Dec 1, 2021

Codecov Report

Merging #1960 (556f350) into master (d7fe61b) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1960   +/-   ##
=======================================
  Coverage   98.81%   98.81%           
=======================================
  Files         225      226    +1     
  Lines       17312    17346   +34     
=======================================
+ Hits        17107    17141   +34     
  Misses        205      205           
Impacted Files Coverage Δ
pennylane/transforms/__init__.py 100.00% <ø> (ø)
pennylane/transforms/optimization/__init__.py 100.00% <100.00%> (ø)
pennylane/transforms/optimization/undo_swaps.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d7fe61b...556f350. Read the comment docs.

@josh146
Copy link
Member

josh146 commented Dec 1, 2021

very nice solution!

@KetpuntoG
Copy link
Contributor Author

very nice solution!

Thanks @josh146 😊

@KetpuntoG
Copy link
Contributor Author

[sc-11831]

Copy link
Contributor

@dime10 dime10 left a comment

Choose a reason for hiding this comment

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

Hey @KetpuntoG, thank you for contributing this transform, great idea for an optimization :)

A few minor comments below.

pennylane/transforms/optimization/undo_swaps.py Outdated Show resolved Hide resolved
pennylane/transforms/__init__.py Show resolved Hide resolved
tests/transforms/test_optimization/test_undo_swaps.py Outdated Show resolved Hide resolved
tests/transforms/test_optimization/test_undo_swaps.py Outdated Show resolved Hide resolved
pennylane/transforms/optimization/undo_swaps.py Outdated Show resolved Hide resolved
tests/transforms/test_optimization/test_undo_swaps.py Outdated Show resolved Hide resolved
KetpuntoG and others added 2 commits December 2, 2021 10:57
Co-authored-by: David Ittah <dime10@users.noreply.github.com>
@KetpuntoG KetpuntoG merged commit 3b51f48 into master Dec 2, 2021
@KetpuntoG KetpuntoG deleted the native_permute branch December 2, 2021 16:19
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.

None yet

3 participants