This repository has been archived by the owner on Dec 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 380
Added to_gate method on certain operators #1299
Closed
shifubear
wants to merge
17
commits into
qiskit-community:master
from
shifubear:adding-to-gate-operator
Closed
Changes from 16 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
40ec6ee
Completed initial draft and tests
shifubear 5f5a00c
Created release note
shifubear 71c772b
Fixed matrix operator to_gate method
shifubear 2ba201f
Passed lint tests
shifubear 060f458
Merge branch 'master' into adding-to-gate-operator
manoelmarques 293e7e2
Merge branch 'master' into adding-to-gate-operator
manoelmarques ae73044
Resolved mypy error with List type
shifubear 3316fdf
Resolved merge
shifubear c5182ed
Fixed lint issues
shifubear f51f03c
Added lint ignore for known pylint error
shifubear 3aab7ec
Disabled pylint for line
shifubear e2e2ee6
Used UnitaryGate in to_gate method instead
shifubear f850448
Added to_gate method to other operators
shifubear 16fc72c
Added imports
shifubear 940b608
Merge branch 'master' into adding-to-gate-operator
manoelmarques 4271dcb
Merge branch 'master' into adding-to-gate-operator
woodsp-ibm 6fa061a
Merge branch 'master' into adding-to-gate-operator
woodsp-ibm 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 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 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 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 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 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,7 @@ | ||
features: | ||
- | | ||
Implemented a ``to_gate`` method to ``CircuitOp``, ``PauliOp``, | ||
``EvolvedOp``, and ``MatrixOp``. This method converts operators to | ||
``Gate`` objects as requested in #1091. If the operator is not unitary, | ||
this method throws an error. | ||
|
This file contains 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 |
---|---|---|
|
@@ -24,7 +24,9 @@ | |
|
||
from qiskit import QiskitError | ||
from qiskit.aqua import AquaError | ||
from qiskit.circuit import QuantumCircuit, QuantumRegister, Instruction, Parameter, ParameterVector | ||
from qiskit.circuit import ( | ||
QuantumCircuit, QuantumRegister, Instruction, Parameter, ParameterVector, Gate | ||
) | ||
|
||
from qiskit.extensions.exceptions import ExtensionError | ||
from qiskit.quantum_info import Operator, Pauli, Statevector | ||
|
@@ -211,6 +213,29 @@ def test_circuit_op_to_matrix(self): | |
np.testing.assert_array_almost_equal( | ||
qcop.to_matrix(), scipy.linalg.expm(-0.5j * Z.to_matrix())) | ||
|
||
def test_circuit_op_to_gate(self): | ||
""" test CircuitOp.to_gate """ | ||
qc = QuantumCircuit(1) | ||
qc.rz(1.0, 0) | ||
qcop = CircuitOp(qc) | ||
with self.subTest('assert to_gate returns Gate'): | ||
self.assertIsInstance(qcop.to_gate(), Gate) | ||
|
||
def test_evolved_op_to_gate(self): | ||
""" test EvolvedOp.to_gate """ | ||
from qiskit.extensions.hamiltonian_gate import HamiltonianGate | ||
|
||
matop = (H ^ 3).to_matrix_op() | ||
evolved_op = EvolvedOp(matop) | ||
with self.subTest('assert to_gate returns Hamiltonian Gate'): | ||
self.assertIsInstance(evolved_op.to_gate(time=1), HamiltonianGate) | ||
|
||
matop = (X ^ Y ^ H ^ T).to_matrix_op() | ||
evolved_op = EvolvedOp(matop) | ||
with self.subTest("assert to_gate doesn't work for non Hermitian input"): | ||
with self.assertRaises(ExtensionError): | ||
evolved_op.to_gate(time=1) | ||
|
||
def test_matrix_to_instruction(self): | ||
"""Test MatrixOp.to_instruction yields an Instruction object.""" | ||
matop = (H ^ 3).to_matrix_op() | ||
|
@@ -222,6 +247,23 @@ def test_matrix_to_instruction(self): | |
with self.assertRaises(ExtensionError): | ||
matop.to_instruction() | ||
|
||
def test_matrix_to_gate(self): | ||
"""Test MatrixOp.to_gate yields a Gate object.""" | ||
matop = (H ^ 3).to_matrix_op() | ||
with self.subTest('assert to_gate returns Gate'): | ||
self.assertIsInstance(matop.to_gate(), Gate) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there some check we can do on the gate to make sure that the conversion code in to_gate() is working beyond just checking its type. Can we check the value in some way - for instance doing to_matrix on the gate and assuring this is as expected too, or whatever seems reasonable to check that the gate is what we expect beyond just checking type. |
||
|
||
matop = ((H ^ 3) + (Z ^ 3)).to_matrix_op() | ||
with self.subTest('non unitary matrix operator throws error'): | ||
with self.assertRaises(ExtensionError): | ||
matop.to_gate() | ||
|
||
def test_pauli_op_to_gate(self): | ||
"""Test PauliOp.to_gate yields a Gate object.""" | ||
pauli_op = (X ^ Y ^ Z) | ||
with self.subTest('assert to_gate returns Gate'): | ||
self.assertIsInstance(pauli_op.to_gate(), Gate) | ||
|
||
def test_adjoint(self): | ||
""" adjoint test """ | ||
gnarly_op = 3 * (H ^ I ^ Y).compose(X ^ X ^ Z).tensor(T ^ Z) + \ | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this take care of
self.coeff
and factor it intoself.primitive
similar to how this is done into_instruction
above.Can you can check this aspect on the other
to_gate
implementations. It maybe for some like circuit_op, like in its to_instruction, that its not feasible to include the coeff on the conversion.