Skip to content

Setting strict=False in QuantumCircuit.assign_parameters has no effect #13933

@mrossinek

Description

@mrossinek

Environment

  • Qiskit version: main @ b9bdc5a
  • Python version: Any
  • Operating system: Any

What is happening?

Specifying strict=False as part of a QuantumCircuit.assign_parameters call does not ignore provided parameters that do not exist in the circuit (contradicting what the documentation says).

Quoting from the docs:

strict – If False, any parameters given in the mapping that are not used in the circuit will be ignored.

How can we reproduce the issue?

from qiskit.circuit import QuantumCircuit

qc = QuantumCircuit(2)
qc.assign_parameters({"a": 1.0}, strict=False)

This produces the error:

Traceback (most recent call last):
  File "/home/oss/Files/Dev/Qiskit/qiskit/main/tmp-assign-strict.py", line 4, in <module>
    qc.assign_parameters({"a": 1.0}, strict=False)
  File "/home/oss/Files/Dev/Qiskit/qiskit/main/qiskit/circuit/quantumcircuit.py", line 4267, in assign_parameters
    raw_mapping = parameters if flat_input else self._unroll_param_dict(parameters)
                                                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/oss/Files/Dev/Qiskit/qiskit/main/qiskit/circuit/quantumcircuit.py", line 4357, in _unroll_param_dict
    out[self.get_parameter(parameter)] = value
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/oss/Files/Dev/Qiskit/qiskit/main/qiskit/circuit/quantumcircuit.py", line 2609, in get_parameter
    raise KeyError(f"no parameter named '{name}' is present")
KeyError: "no parameter named 'a' is present"

What should happen?

The error should not occur.

Any suggestions?

The strict argument could either be forwarded to QuantumCircuit._unroll_param_dict here:

raw_mapping = parameters if flat_input else self._unroll_param_dict(parameters)

And then handled inside of that internal method.

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions