In the current release of Cirq (0.4.0) there is a strong limitation on symbols: you can't scale them or add them (Why? We were worried about being pulled down the rabbit hole of implementing a whole symbolic algebra system.). Making matters worse, Cirq internally works in radians divided by pi to avoid some minor sources of floating point error. So when you write cirq.Rx(cirq.Symbol("θ"))
it internally attempts to scale the argument, and you get an error about multiplication/division not being supported for symbols.
There is an active pull request to fix this by delegating all symbolic work to sympy. Once that's merged, the next release will allow you to write cirq.Rx(sympy.Symbol("θ"))
whereas currently you can only use symbols in exponents e.g. cirq.X**cirq.Symbol("t")
.
So, currently in 0.4.0, you can do this:
import cirq
import matplotlib.pyplot as plt
q = cirq.LineQubit(0)
circuit = cirq.Circuit.from_ops(
cirq.X(q)**cirq.Symbol('angle_over_pi'),
cirq.measure(q),
)
results = cirq.Simulator().run_sweep(
circuit,
params=cirq.Linspace(key='angle_over_pi', start=0, stop=2, length=20),
repetitions=1000)
plt.plot([r.histogram(key=str(q))[1] for r in results])
plt.show()

But you'll need to wait until 0.5.0 to be able to do this extremely similar thing using Rx
instead of X**
and sympy.Symbol
instead of cirq.Symbol
(which will be gone):
import cirq
import matplotlib.pyplot as plt
import numpy as np
q = cirq.LineQubit(0)
circuit = cirq.Circuit.from_ops(
cirq.Rx(sympy.Symbol('rads')).on(q),
cirq.measure(q),
)
results = cirq.Simulator().run_sweep(
circuit,
params=cirq.Linspace(key='rads', start=0, stop=2*np.pi, length=20),
repetitions=1000)
plt.plot([r.histogram(key=q)[1] for r in results])
plt.show()