-
Notifications
You must be signed in to change notification settings - Fork 3k
Implement the relative phase Toffoli gates #3761
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
Merged
Merged
Changes from 16 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
37f56ca
implement relative phase toffoli gates as gates
Cryoris 65c5855
fix CX, Cnot mixup
Cryoris cd6813b
update init, remove unused import
Cryoris 80bca66
add matrix repr, fix num_qubits
Cryoris bc8753e
add test on relative phase toffolis
Cryoris 16ecfa4
Merge branch 'master' of github.com:Qiskit/qiskit-terra into relative…
Cryoris 21f5546
remove base_gate, X does not really apply
Cryoris 9cbcbd8
make Gate, not ControlledGate, update docstrings
Cryoris 7ef0260
rename to RCCX
Cryoris f8e4dbc
Merge branch 'master' of github.com:Qiskit/qiskit-terra into relative…
Cryoris 74a189f
add qasm definitions
Cryoris 4f3c617
update str length due to rcc(c)x definitions
Cryoris 03a28ae
fix deprecation warning and test
Cryoris ba64e65
Merge branch 'master' into relative_phase_toffoli_gates
Cryoris e08c136
Merge branch 'master' into relative_phase_toffoli_gates
Cryoris 0ca3239
Merge branch 'relative_phase_toffoli_gates' of github.com:Cryoris/qis…
Cryoris 46b03b7
Correct docstring
Cryoris 3e5027d
Correct docstring
Cryoris 3371db3
rename tgt/ctl to full names
Cryoris 7ca14af
Merge branch 'master' into relative_phase_toffoli_gates
Cryoris 2fcd425
Merge branch 'master' into relative_phase_toffoli_gates
ajavadia File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,116 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2019. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """The simplified 3-controlled Toffoli gate.""" | ||
|
|
||
| import numpy | ||
|
|
||
| from qiskit.circuit import QuantumCircuit, Gate, QuantumRegister | ||
| from qiskit.extensions.standard.u1 import U1Gate | ||
| from qiskit.extensions.standard.u2 import U2Gate | ||
| from qiskit.extensions.standard.x import CnotGate | ||
| from qiskit.qasm import pi | ||
|
|
||
|
|
||
| class RCCCXGate(Gate): | ||
| """The simplified 3-controlled Toffoli gate. | ||
|
|
||
| The simplified Toffoli gate implements the Toffoli gate up to relative phases. | ||
| Note, that the simplified Toffoli is not equivalent to the Toffoli. But can be used in places | ||
| where the Toffoli gate is uncomputed again. | ||
|
|
||
| This concrete implementation is from https://arxiv.org/abs/1508.03273, the complete circuit | ||
| of Fig. 4. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| """Create a new PCCCX gate.""" | ||
| super().__init__('rcccx', 4, []) | ||
|
|
||
| def _define(self): | ||
| """ | ||
| gate rcccx a,b,c,d | ||
| { u2(0,pi) d; | ||
| u1(pi/4) d; | ||
| cx c,d; | ||
| u1(-pi/4) d; | ||
| u2(0,pi) d; | ||
| cx a,d; | ||
| u1(pi/4) d; | ||
| cx b,d; | ||
| u1(-pi/4) d; | ||
| cx a,d; | ||
| u1(pi/4) d; | ||
| cx b,d; | ||
| u1(-pi/4) d; | ||
| u2(0,pi) d; | ||
| u1(pi/4) d; | ||
| cx c,d; | ||
| u1(-pi/4) d; | ||
| u2(0,pi) d; | ||
| } | ||
| """ | ||
| definition = [] | ||
| q = QuantumRegister(4, 'q') | ||
|
|
||
| rule = [ | ||
| (U2Gate(0, pi), [q[3]], []), # H gate | ||
| (U1Gate(pi / 4), [q[3]], []), # T gate | ||
| (CnotGate(), [q[2], q[3]], []), | ||
| (U1Gate(-pi / 4), [q[3]], []), # inverse T gate | ||
| (U2Gate(0, pi), [q[3]], []), | ||
| (CnotGate(), [q[0], q[3]], []), | ||
| (U1Gate(pi / 4), [q[3]], []), | ||
| (CnotGate(), [q[1], q[3]], []), | ||
| (U1Gate(-pi / 4), [q[3]], []), | ||
| (CnotGate(), [q[0], q[3]], []), | ||
| (U1Gate(pi / 4), [q[3]], []), | ||
| (CnotGate(), [q[1], q[3]], []), | ||
| (U1Gate(-pi / 4), [q[3]], []), | ||
| (U2Gate(0, pi), [q[3]], []), | ||
| (U1Gate(pi / 4), [q[3]], []), | ||
| (CnotGate(), [q[2], q[3]], []), | ||
| (U1Gate(-pi / 4), [q[3]], []), | ||
| (U2Gate(0, pi), [q[3]], []), | ||
| ] | ||
| for inst in rule: | ||
| definition.append(inst) | ||
| self.definition = definition | ||
|
|
||
| def to_matrix(self): | ||
| """Return a numpy.array for the PCCCX gate.""" | ||
|
Cryoris marked this conversation as resolved.
Outdated
|
||
| return numpy.array([[1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| [0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| [0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| [0, 0, 0, 1j, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| [0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| [0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| [0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0], | ||
| [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1], | ||
| [0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0], | ||
| [0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0], | ||
| [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0], | ||
| [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1j, 0, 0, 0, 0], | ||
| [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0], | ||
| [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0], | ||
| [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0], | ||
| [0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=complex) | ||
|
|
||
|
|
||
| def rcccx(self, ctl1, ctl2, ctl3, tgt): | ||
|
Cryoris marked this conversation as resolved.
Outdated
|
||
| """Apply the simplified, relative-phase 3-control Toffoli gate.""" | ||
| return self.append(RCCCXGate(), [ctl1, ctl2, ctl3, tgt], []) | ||
|
|
||
|
|
||
| QuantumCircuit.rcccx = rcccx | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| # -*- coding: utf-8 -*- | ||
|
|
||
| # This code is part of Qiskit. | ||
| # | ||
| # (C) Copyright IBM 2020. | ||
| # | ||
| # This code is licensed under the Apache License, Version 2.0. You may | ||
| # obtain a copy of this license in the LICENSE.txt file in the root directory | ||
| # of this source tree or at http://www.apache.org/licenses/LICENSE-2.0. | ||
| # | ||
| # Any modifications or derivative works of this code must retain this | ||
| # copyright notice, and modified files need to carry a notice indicating | ||
| # that they have been altered from the originals. | ||
|
|
||
| """The simplified Toffoli gate.""" | ||
|
|
||
| import numpy | ||
|
|
||
| from qiskit.circuit import QuantumCircuit, Gate, QuantumRegister | ||
| from qiskit.extensions.standard.u1 import U1Gate | ||
| from qiskit.extensions.standard.u2 import U2Gate | ||
| from qiskit.extensions.standard.x import CnotGate | ||
| from qiskit.qasm import pi | ||
|
|
||
|
|
||
| class RCCXGate(Gate): | ||
| """The simplified Toffoli gate, also referred to as Margolus gate. | ||
|
|
||
| The simplified Toffoli gate implements the Toffoli gate up to relative phases. | ||
| This implementation requires three CX gates which is the minimal amount possible, | ||
| as shown in https://arxiv.org/abs/quant-ph/0312225. | ||
| Note, that the simplified Toffoli is not equivalent to the Toffoli. But can be used in places | ||
| where the Toffoli gate is uncomputed again. | ||
|
|
||
| This concrete implementation is from https://arxiv.org/abs/1508.03273, the dashed box | ||
| of Fig. 3. | ||
| """ | ||
|
|
||
| def __init__(self): | ||
| """Create a new simplified CCX gate.""" | ||
| super().__init__('rccx', 3, []) | ||
|
|
||
| def _define(self): | ||
| """ | ||
| gate rccx a,b,c | ||
| { u2(0,pi) c; | ||
| u1(pi/4) c; | ||
| cx b, c; | ||
| u1(-pi/4) c; | ||
| cx a, c; | ||
| u1(pi/4) c; | ||
| cx b, c; | ||
| u1(-pi/4) c; | ||
| u2(0,pi) c; | ||
| } | ||
| """ | ||
| definition = [] | ||
| q = QuantumRegister(3, 'q') | ||
| rule = [ | ||
| (U2Gate(0, pi), [q[2]], []), # H gate | ||
| (U1Gate(pi / 4), [q[2]], []), # T gate | ||
| (CnotGate(), [q[1], q[2]], []), | ||
| (U1Gate(-pi / 4), [q[2]], []), # inverse T gate | ||
| (CnotGate(), [q[0], q[2]], []), | ||
| (U1Gate(pi / 4), [q[2]], []), | ||
| (CnotGate(), [q[1], q[2]], []), | ||
| (U1Gate(-pi / 4), [q[2]], []), # inverse T gate | ||
| (U2Gate(0, pi), [q[2]], []), # H gate | ||
| ] | ||
| for inst in rule: | ||
| definition.append(inst) | ||
| self.definition = definition | ||
|
|
||
| def to_matrix(self): | ||
| """Return a numpy.array for the simplified CCX gate.""" | ||
| return numpy.array([[1, 0, 0, 0, 0, 0, 0, 0], | ||
| [0, 1, 0, 0, 0, 0, 0, 0], | ||
| [0, 0, 1, 0, 0, 0, 0, 0], | ||
| [0, 0, 0, 0, 0, 0, 0, -1j], | ||
| [0, 0, 0, 0, 1, 0, 0, 0], | ||
| [0, 0, 0, 0, 0, -1, 0, 0], | ||
| [0, 0, 0, 0, 0, 0, 1, 0], | ||
| [0, 0, 0, 1j, 0, 0, 0, 0]], dtype=complex) | ||
|
|
||
|
|
||
| def rccx(self, ctl1, ctl2, tgt): | ||
|
Cryoris marked this conversation as resolved.
Outdated
|
||
| """Apply the simplified, relative-phase Toffoli gate.""" | ||
| return self.append(RCCXGate(), [ctl1, ctl2, tgt], []) | ||
|
|
||
|
|
||
| QuantumCircuit.rccx = rccx | ||
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.