From 9233356720d5867e51b481d960bed0227de6e9b1 Mon Sep 17 00:00:00 2001 From: Mikhail Andrenkov Date: Fri, 23 Feb 2024 09:54:40 -0500 Subject: [PATCH 1/4] Disable strict parameter assignment for QuantumCircuits --- pennylane_qiskit/converter.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pennylane_qiskit/converter.py b/pennylane_qiskit/converter.py index 79d447ce9..a04976800 100644 --- a/pennylane_qiskit/converter.py +++ b/pennylane_qiskit/converter.py @@ -250,7 +250,8 @@ def _check_circuit_and_assign_parameters( # we must remove them from the binding dictionary before binding. del params[k] - return quantum_circuit.assign_parameters(params) + # Disabling "strict" assignment allows extra parameters to be ignored. + return quantum_circuit.assign_parameters(params, strict=False) def _get_operation_params(instruction, unbound_params) -> list: From 27787233c780a8098401238b2b00a23d82e3aa3e Mon Sep 17 00:00:00 2001 From: Mikhail Andrenkov Date: Fri, 23 Feb 2024 09:56:53 -0500 Subject: [PATCH 2/4] Update tests to reflect new parameter behaviour --- tests/test_converter.py | 47 ++++++++++++++++++++++------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/tests/test_converter.py b/tests/test_converter.py index 3917582dc..5fc8ca3ef 100644 --- a/tests/test_converter.py +++ b/tests/test_converter.py @@ -254,38 +254,41 @@ def test_parameter_was_not_bound(self, recorder): ): _check_parameter_bound(theta, unbound_params) - def test_extra_parameters_were_passed(self, recorder): - """Tests that loading raises an error when extra parameters were - passed.""" - - theta = Parameter("ฮธ") - phi = Parameter("ฯ†") - x = np.tensor(0.5, requires_grad=False) - y = np.tensor(0.3, requires_grad=False) + def test_unused_parameters_are_ignored(self, recorder): + """Tests that unused parameters are ignored during assignment.""" + a, b, c = [Parameter(var) for var in "abc"] + v = ParameterVector("v", 2) - qc = QuantumCircuit(3, 1) + qc = QuantumCircuit(1) + qc.rz(a, [0]) quantum_circuit = load(qc) - with pytest.raises(QiskitError): - with recorder: - quantum_circuit(params={theta: x, phi: y}) + with recorder: + quantum_circuit(params={a: 0.1, b: 0.2, c: 0.3, v: [0.4, 0.5]}) - def test_quantum_circuit_error_passing_parameters_not_required(self, recorder): - """Tests the load method raises a QiskitError if arguments - that are not required were passed.""" + assert len(recorder.queue) == 1 + assert recorder.queue[0].name == "RZ" + assert recorder.queue[0].parameters == [0.1] + assert recorder.queue[0].wires == Wires([0]) - theta = Parameter("ฮธ") - angle = np.tensor(0.5, requires_grad=False) + def test_unused_parameter_vector_items_are_ignored(self, recorder): + """Tests that unused parameter vector items are ignored during assignment.""" + a, b = [Parameter(var) for var in "ab"] + v = ParameterVector("v", 3) - qc = QuantumCircuit(3, 1) - qc.z([0]) + qc = QuantumCircuit(1) + qc.rz(v[1], [0]) quantum_circuit = load(qc) - with pytest.raises(QiskitError): - with recorder: - quantum_circuit(params={theta: angle}) + with recorder: + quantum_circuit(params={a: 0.1, b: 0.2, v: [0.3, 0.4, 0.5]}) + + assert len(recorder.queue) == 1 + assert recorder.queue[0].name == "RZ" + assert recorder.queue[0].parameters == [0.4] + assert recorder.queue[0].wires == Wires([0]) def test_quantum_circuit_error_not_qiskit_circuit_passed(self, recorder): """Tests the load method raises a ValueError, if something From 92bf300560a6b6dbd324ea2763b798c8375afd2f Mon Sep 17 00:00:00 2001 From: Mikhail Andrenkov Date: Fri, 23 Feb 2024 09:57:22 -0500 Subject: [PATCH 3/4] Ensure `qiskit-terra` version is consistent with `qiskit` --- requirements.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/requirements.txt b/requirements.txt index b36af16c2..ac3dfc285 100644 --- a/requirements.txt +++ b/requirements.txt @@ -29,7 +29,7 @@ qiskit-aer==0.13.3 qiskit-ibm-runtime==0.20.0 qiskit-ibm-provider==0.10.0 qiskit-ignis==0.7.1 -qiskit-terra==0.46.0 +qiskit-terra==0.45.3 requests==2.31.0 requests-ntlm==1.2.0 retworkx==0.14.0 From 4645fe4c3a5997a688037de8325a4e5c0ef6b603 Mon Sep 17 00:00:00 2001 From: Mikhail Andrenkov Date: Fri, 23 Feb 2024 12:27:22 -0500 Subject: [PATCH 4/4] Add note to changelog --- CHANGELOG.md | 38 +++++++++++++++++++++----------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7b84f7160..2a5442675 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,15 +4,15 @@ ### Improvements ๐Ÿ›  -* The UI for passing parameters to a ``qfunc`` generated when loading a Qiskit ``QuantumCircuit`` - into PennyLane is updated to allow passing parameters as args or kwargs, rather than as +* The UI for passing parameters to a ``qfunc`` generated when loading a Qiskit ``QuantumCircuit`` + into PennyLane is updated to allow passing parameters as args or kwargs, rather than as a dictionary. The old dictionary UI continues to be supported. [(#406)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/406) [(#428)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/428) * Measurement operations are now added to the PennyLane template when a ``QuantumCircuit`` is converted using `load`. Additionally, one can override any existing terminal - measurements by providing a list of PennyLane + measurements by providing a list of PennyLane `measurements `_ themselves. [(#405)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/405) @@ -29,6 +29,9 @@ ``Barrier``, ``CYGate``, ``CHGate``, ``CPhase``, ``CCZGate``, ``ECRGate``, and ``GlobalPhaseGate``. [(#449)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/449) +* Unused parameters are now ignored when a `QuantumCircuit` is converted using `load`. + [(#454)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/454) + ### Breaking changes ๐Ÿ’” ### Deprecations ๐Ÿ‘‹ @@ -41,6 +44,7 @@ This release contains contributions from (in alphabetical order): +Mikhail Andrenkov Utkarsh Azad Lillian Frederiksen @@ -49,18 +53,18 @@ Lillian Frederiksen ### Bug fixes ๐Ÿ› -* The kwargs `job_tags` and `session_id` are passed to the correct arguments in the - `circuit_runner` device so that they will be used in the Qiskit backend; these +* The kwargs `job_tags` and `session_id` are passed to the correct arguments in the + `circuit_runner` device so that they will be used in the Qiskit backend; these were previously ignored. [(#358)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/358) -* The `generate_samples` method for the `IBMQSamplerDevice` is updated to get counts - from the nearest probability distribution rather than the quasi-distribution (which - may contain negative probabilities and therefore raise errors). +* The `generate_samples` method for the `IBMQSamplerDevice` is updated to get counts + from the nearest probability distribution rather than the quasi-distribution (which + may contain negative probabilities and therefore raise errors). [(#357)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/357) - -* The `generate_samples` method for the `IBMQSamplerDevice` now avoids raising an - indexing error when some states are not populated, and labels states according to + +* The `generate_samples` method for the `IBMQSamplerDevice` now avoids raising an + indexing error when some states are not populated, and labels states according to the Pennylane convention instead of Qiskit convention. [(#357)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/357) @@ -83,7 +87,7 @@ Francesco Scala ### Bug fixes ๐Ÿ› -* Update conversion of PennyLane to Qiskit operators to accommodate +* Update conversion of PennyLane to Qiskit operators to accommodate the addition of Singleton classes in the newest version of Qiskit. [(#347)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/347) @@ -315,10 +319,10 @@ Mikhail Andrenkov, Christina Lee, Romain Moyard, Antal Szรกva * Fix runtime sampler due to changes on Qiskit side. [(#201)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/201) - + * Pin `jinja2` to 3.0.3 because of sphinx incompatibility. [(#207)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/207) - + ### Contributors This release contains contributions from (in alphabetical order): @@ -326,7 +330,7 @@ This release contains contributions from (in alphabetical order): Samuel Banning, Romain Moyard --- - + # Release 0.22.0 ### Improvements @@ -334,7 +338,7 @@ Samuel Banning, Romain Moyard * Changed a validation check such that it handles qubit numbers represented as strings. [(#184)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/184) - + * Changed the VQE callback function for SciPy optimizers. [(#187)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/187) @@ -346,7 +350,7 @@ Samuel Banning, Romain Moyard * Changed the access to Hamiltonian terms `hamiltonian.terms()` as a method. [(#190)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/190) - + ### Contributors This release contains contributions from (in alphabetical order):