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

Ignore unused parameters on load() #454

Merged
merged 4 commits into from
Feb 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 21 additions & 17 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <https://docs.pennylane.ai/en/stable/introduction/measurements.html>`_ themselves.
[(#405)](https://github.com/PennyLaneAI/pennylane-qiskit/pull/405)

Expand All @@ -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 👋
Expand All @@ -41,6 +44,7 @@

This release contains contributions from (in alphabetical order):

Mikhail Andrenkov
Utkarsh Azad
Lillian Frederiksen

Expand All @@ -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)

Expand All @@ -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)

Expand Down Expand Up @@ -315,26 +319,26 @@ 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):

Samuel Banning, Romain Moyard

---

# Release 0.22.0

### Improvements

* 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)

Expand All @@ -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):
Expand Down
3 changes: 2 additions & 1 deletion pennylane_qiskit/converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
47 changes: 25 additions & 22 deletions tests/test_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading