0
$\begingroup$

Is there a way to use a ParamResolver or Sweep to assign values to the symbols in a parameterized Cirq circuit without running it?

For example, something like:

#set up parameterized circuit
simulator = cirq.Simulator()
circuit = cirq.Circuit()
alpha = sympy.Symbol('alpha')
beta = sympy.Symbol('beta')
circuit.append(one_step(alpha, beta))
circuit.append(cirq.measure(*qubits, key='x'))
sweep = (cirq.Linspace(key='alpha', start=0.1, stop=0.9, length=5)
         * cirq.Linspace(key='beta', start=0.1, stop=0.9, length=5))
for param_resolver in study.to_resolvers(sweep):
     #resolve parameters
     *line that resolves the symbolic parameters in circuit using param_resolver*
     #run measurement simulations 
     measurements = simulator.run(circuit=circuit, repetitions=100)

Instead of:

#set up parametrized circuit
simulator = cirq.Simulator()
circuit = cirq.Circuit()
alpha = sympy.Symbol('alpha')
beta = sympy.Symbol('beta')
circuit.append(one_step(alpha, beta))
circuit.append(cirq.measure(*qubits, key='x'))
sweep = (cirq.Linspace(key='alpha', start=0.1, stop=0.9, length=5)
         * cirq.Linspace(key='beta', start=0.1, stop=0.9, length=5))
for param_resolver in study.to_resolvers(sweep):
     #resolve parameters and run measurement simulations in one step
     measurements = simulator.run(circuit=circuit, param_resolver=param_resolver, repetitions=100)
$\endgroup$
1
$\begingroup$

You're looking for cirq.resolve_parameters(object, resolver), which returns the object after the parameters have been resolved. For example:

import cirq
import sympy

q = cirq.LineQubit(0)
s = sympy.Symbol('s')

symbol_op = cirq.X(q)**s
resolved_op = cirq.resolve_parameters(symbol_op, {s: 0.25})
print(resolved_op)
# X**0.25(0)

symbol_circuit = cirq.Circuit(symbol_op, cirq.measure(q))
resolved_circuit = cirq.resolve_parameters(symbol_circuit, {s: 0.5})
print(resolved_circuit)
# 0: ───X^0.5───M───
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.