Skip to content

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

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

Open
mrossinek opened this issue Feb 28, 2025 · 7 comments · May be fixed by #14365
Open

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

mrossinek opened this issue Feb 28, 2025 · 7 comments · May be fixed by #14365
Assignees
Labels
bug Something isn't working

Comments

@mrossinek
Copy link
Member

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.

@mrossinek mrossinek added the bug Something isn't working label Feb 28, 2025
@jakelishman
Copy link
Member

Thanks Max - I think this might be specific to string inputs? They got added after strict, iirc.

@mrossinek
Copy link
Member Author

I think this might be specific to string inputs? They got added after strict, iirc.

Yes, you are right. Using a Parameter object works as expected.

from qiskit.circuit import Parameter, QuantumCircuit

qc = QuantumCircuit(2)
a = Parameter("a")
print("Testing assignment by parameter")
qc.assign_parameters({a: 1.0}, strict=False)
print("Testing assignment by string")
qc.assign_parameters({"a": 1.0}, strict=False)

Outputs:

Testing assignment by parameter
Testing assignment by string
Traceback (most recent call last):
  File "/home/oss/Files/Dev/Qiskit/qiskit/main/tmp-assign-strict.py", line 8, 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"

I.e. only the last line of the code (with the string-based parameter assignment) fails

@Raghav-Bell
Copy link
Contributor

hi @jakelishman
Can you please assign this issue to me? I would be happy to close this issue.
Thanks

@kaldag
Copy link

kaldag commented May 13, 2025

Hi @jakelishman
I would be happy to take this, if its not been solved already.

@jakelishman
Copy link
Member

Thanks Kalyan - I'll assign you, and we'll start getting the permissions set up to make you a member of the Qiskit organisation.

@Raghav-Bell: sorry, I'd not seen that message before, and now I need to assign the issue internally. Sorry for the lack of response.

@kaldag
Copy link

kaldag commented May 14, 2025

@jakelishman do you expect the argument to be ignored when strict = False, irrespective of whether it is a Parameter type or a string type? A string input is an error anyway right.

@jakelishman
Copy link
Member

Kalyan: strings are acceptable arguments; they're looked up with get_parameter. Normally we just raise the error if there's no parameter of the corresponding name, but if strict=False we shouldn't raise an error, and ignore it instead. Actually incorrect types (like an int or something weird) can just continue to throw whatever exception they eventually do, even in strict=False.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants