-
Notifications
You must be signed in to change notification settings - Fork 2.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
QuantumCircuit.data modification methods to reset ParameterTable. (#4319
) Updates `QuantumCircuit.inverse`, `QuantumCircuit.mirror` and the `QuantumCircuit.data` setter to clear the internal `ParameterTable` instance prior to rebuilding the `_data` list. Also, updates `mirror` and `inverse` to prefer `_append` to modifying `_data` directly, and adds test.python.circuit.test_parameters. raise_if_parameter_table_invalid to validate if a `ParameterTable` and its `QuantumCircuit` are consistent. Additionally, the `inverse method` of the `qiskit.circuit.library.basis_change.qft` circuit has been updated to keep the requirement that the returned circuit be a `QFT` instance. Resolves #4235 . Demonstration of issue: ``` >>> qc = qk.QuantumCircuit(1) >>> x = qk.circuit.Parameter('x') >>> qc.rz(x, 0) <qiskit.circuit.instructionset.InstructionSet object at 0x12cb0d780> >>> qc.data[0] (<qiskit.extensions.standard.rz.RZGate object at 0x12bd2fb38>, [Qubit(QuantumRegister(1, 'q'), 0)], []) >>> qc._parameter_table ParameterTable({Parameter(x): [(<qiskit.extensions.standard.rz.RZGate object at 0x12bd2fb38>, 0)]}) >>> >>> >>> inv = qc.inverse() >>> inv.data[0] (<qiskit.extensions.standard.rz.RZGate object at 0x12c9c9278>, [Qubit(QuantumRegister(1, 'q'), 0)], []) >>> inv._parameter_table ParameterTable({Parameter(x): [(<qiskit.extensions.standard.rz.RZGate object at 0x12cc84da0>, 0)]}) >>> >>> inv.copy() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/circuit/quantumcircuit.py", line 1226, in copy for param in self._parameter_table File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/circuit/quantumcircuit.py", line 1226, in <dictcomp> for param in self._parameter_table File "/Users/kevin.krsulichibm.com/q/qiskit-terra/qiskit/circuit/quantumcircuit.py", line 1225, in <listcomp> for instr, param_index in self._parameter_table[param]] KeyError: 5046291872 >>> >>> >>> mirror = qc.mirror() >>> mirror.data[0] (<qiskit.extensions.standard.rz.RZGate object at 0x12d3c4630>, [Qubit(QuantumRegister(1, 'q'), 0)], []) >>> mirror._parameter_table ParameterTable({Parameter(x): [(<qiskit.extensions.standard.rz.RZGate object at 0x12bd2f6a0>, 0), (<qiskit.extensions.standard.rz.RZGate object at 0x12d3c4630>, 0)]}) >>> >>> >>> qc.data = [] >>> qc._parameter_table ParameterTable({Parameter(x): [(<qiskit.extensions.standard.rz.RZGate object at 0x12bd2fb38>, 0)]}) ``` Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
- Loading branch information
1 parent
b30f22c
commit 8629433
Showing
4 changed files
with
134 additions
and
8 deletions.
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
7 changes: 7 additions & 0 deletions
7
...5-to_instruction-call-throws-error-on-parametrized-inverted-circuit-acf38e3ddd111900.yaml
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 @@ | ||
--- | ||
fixes: | ||
- | | ||
As reported in https://github.com/Qiskit/qiskit-terra/issues/4235 , the | ||
``QuantumCircuit.inverse``, ``QuantumCircuit.mirror`` methods, as well as | ||
the ``QuantumCircuit.data`` setter would generate an invalid circuit when | ||
used on a parameterized circuit instance. This has been resolved. |
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