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

CircuitOperation crashes simulation if parameters come from the main circuit #5016

Closed
daxfohl opened this issue Feb 23, 2022 · 1 comment · Fixed by #5033
Closed

CircuitOperation crashes simulation if parameters come from the main circuit #5016

daxfohl opened this issue Feb 23, 2022 · 1 comment · Fixed by #5033
Labels
kind/bug-report Something doesn't seem to work.

Comments

@daxfohl
Copy link
Collaborator

daxfohl commented Feb 23, 2022

Description of the issue

If there's a parameterized operation in a subcircuit, it crashes when passing the param resolver into simulate.

How to reproduce the issue

def test_subcircuit_with_params():
    circuit = cirq.Circuit(
        cirq.CircuitOperation(cirq.FrozenCircuit(cirq.X(cirq.LineQubit(0)) ** sympy.Symbol('a')))
    )
    cirq.Simulator().simulate(circuit, param_resolver={'a': 0})

raises

ValueError: Recursive resolution of CircuitOperation parameters is prohibited. Use "recursive=False" to prevent this error

stack trace

cirq-core/cirq/sim/simulator.py:503: in simulate
    return self.simulate_sweep(
cirq-core/cirq/sim/simulator.py:518: in simulate_sweep
    return list(self.simulate_sweep_iter(program, params, qubit_order, initial_state))
cirq-core/cirq/sim/simulator_base.py:351: in simulate_sweep_iter
    yield from super().simulate_sweep_iter(suffix, params, qubit_order, sim_state)
cirq-core/cirq/sim/simulator.py:615: in simulate_sweep_iter
    all_step_results = self.simulate_moment_steps(
cirq-core/cirq/sim/simulator.py:656: in simulate_moment_steps
    resolved_circuit = protocols.resolve_parameters(circuit, param_resolver)
cirq-core/cirq/protocols/resolve_parameters.py:184: in resolve_parameters
    result = getter(param_resolver, recursive)
cirq-core/cirq/circuits/circuit.py:2422: in _resolve_parameters_
    resolved_operations = _resolve_operations(moment.operations, resolver, recursive)
cirq-core/cirq/circuits/circuit.py:2490: in _resolve_operations
    resolved_operations.append(protocols.resolve_parameters(op, param_resolver, recursive))
cirq-core/cirq/protocols/resolve_parameters.py:184: in resolve_parameters
    result = getter(param_resolver, recursive)

Cirq version
0.14.0.dev

@daxfohl daxfohl added the kind/bug-report Something doesn't seem to work. label Feb 23, 2022
@daxfohl
Copy link
Collaborator Author

daxfohl commented Feb 25, 2022

Looks like the root cause is already documented in #3619, however leaving this open for now since that is a feature request while this is a bug.

CirqBot pushed a commit that referenced this issue Mar 1, 2022
Preserves existing behavior in circuitoperations, where `with_params({a: b, b: a})` just swaps the parameter names and preserves that behavior for subsequent application (we don't change like 613), but we allow optional recursive application for each individual resolution applied (line 614). @95-martin-orion 

Fixes #5016
Closes #3619
95-martin-orion pushed a commit to 95-martin-orion/Cirq that referenced this issue Mar 2, 2022
Preserves existing behavior in circuitoperations, where `with_params({a: b, b: a})` just swaps the parameter names and preserves that behavior for subsequent application (we don't change like 613), but we allow optional recursive application for each individual resolution applied (line 614). @95-martin-orion 

Fixes quantumlib#5016
Closes quantumlib#3619
rht pushed a commit to rht/Cirq that referenced this issue May 1, 2023
Preserves existing behavior in circuitoperations, where `with_params({a: b, b: a})` just swaps the parameter names and preserves that behavior for subsequent application (we don't change like 613), but we allow optional recursive application for each individual resolution applied (line 614). @95-martin-orion 

Fixes quantumlib#5016
Closes quantumlib#3619
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this issue Oct 31, 2024
Preserves existing behavior in circuitoperations, where `with_params({a: b, b: a})` just swaps the parameter names and preserves that behavior for subsequent application (we don't change like 613), but we allow optional recursive application for each individual resolution applied (line 614). @95-martin-orion 

Fixes quantumlib#5016
Closes quantumlib#3619
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/bug-report Something doesn't seem to work.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant