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 3 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
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