Skip to content

Commit

Permalink
Bumped Version to cirq 0.8. (tensorflow#234)
Browse files Browse the repository at this point in the history
* Bumped Version to cirq 0.8.

* format.

* oops 0.9

* pinned sympy.

* Fixed notebooks.

* pinned sympy in setup as well.
  • Loading branch information
MichaelBroughton authored May 9, 2020
1 parent 82270b3 commit 41be90d
Show file tree
Hide file tree
Showing 17 changed files with 86 additions and 83 deletions.
8 changes: 4 additions & 4 deletions docs/tutorials/barren_plateaus.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@
" \"\"\"Generate random QNN's with the same structure from McClean et al.\"\"\"\n",
" circuit = cirq.Circuit()\n",
" for qubit in qubits:\n",
" circuit += cirq.Ry(np.pi / 4.0)(qubit)\n",
" circuit += cirq.ry(np.pi / 4.0)(qubit)\n",
"\n",
" for d in range(depth):\n",
" # Add a series of single qubit rotations.\n",
Expand All @@ -209,13 +209,13 @@
" ) * 2.0 * np.pi if i != 0 or d != 0 else symbol\n",
" if random_n > 2. / 3.:\n",
" # Add a Z.\n",
" circuit += cirq.Rz(random_rot)(qubit)\n",
" circuit += cirq.rz(random_rot)(qubit)\n",
" elif random_n > 1. / 3.:\n",
" # Add a Y.\n",
" circuit += cirq.Ry(random_rot)(qubit)\n",
" circuit += cirq.ry(random_rot)(qubit)\n",
" else:\n",
" # Add a X.\n",
" circuit += cirq.Rx(random_rot)(qubit)\n",
" circuit += cirq.rx(random_rot)(qubit)\n",
"\n",
" # Add CZ ladder.\n",
" for src, dest in zip(qubits, qubits[1:]):\n",
Expand Down
6 changes: 3 additions & 3 deletions docs/tutorials/qcnn.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@
"\n",
"<img src=\"./images/qcnn_2.png\" width=\"1000\">\n",
"\n",
"An \"excited\" cluster state is defined as a cluster state that had a `cirq.Rx` gate applied to any of its qubits. Qconv and QPool are discussed later in this tutorial."
"An \"excited\" cluster state is defined as a cluster state that had a `cirq.rx` gate applied to any of its qubits. Qconv and QPool are discussed later in this tutorial."
]
},
{
Expand Down Expand Up @@ -318,7 +318,7 @@
},
"source": [
"### 1.4 Data\n",
"Before building your model, you can generate your data. In this case it's going to be excitations to the cluster state (The original paper uses a more complicated dataset). Excitations are represented with `cirq.Rx` gates. A large enough rotation is deemed an excitation and is labeled `1` and a rotation that isn't large enough is labeled `-1` and deemed not an excitation."
"Before building your model, you can generate your data. In this case it's going to be excitations to the cluster state (The original paper uses a more complicated dataset). Excitations are represented with `cirq.rx` gates. A large enough rotation is deemed an excitation and is labeled `1` and a rotation that isn't large enough is labeled `-1` and deemed not an excitation."
]
},
{
Expand All @@ -339,7 +339,7 @@
" for n in range(n_rounds):\n",
" for bit in qubits:\n",
" rng = np.random.uniform(-np.pi, np.pi)\n",
" excitations.append(cirq.Circuit(cirq.Rx(rng)(bit)))\n",
" excitations.append(cirq.Circuit(cirq.rx(rng)(bit)))\n",
" labels.append(1 if (-np.pi / 2) <= rng <= (np.pi / 2) else -1)\n",
"\n",
" split_ind = int(len(excitations) * 0.7)\n",
Expand Down
2 changes: 1 addition & 1 deletion release/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def finalize_options(self):
self.install_lib = self.install_platlib


REQUIRED_PACKAGES = ['cirq == 0.7.0', 'pathos == 0.2.5']
REQUIRED_PACKAGES = ['cirq == 0.8.0', 'pathos == 0.2.5', 'sympy == 1.4']
CUR_VERSION = '0.3.0'


Expand Down
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
cirq==0.7.0
cirq==0.8.0
sympy==1.4
nbconvert==5.6.1
nbformat==4.4.0
pylint==2.4.4
Expand Down
2 changes: 1 addition & 1 deletion tensorflow_quantum/core/ops/tfq_ps_util_ops_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ def test_empty(self):
def test_rotation_gates(self):
"""Test that rotation gates work."""
bit = cirq.GridQubit(0, 0)
circuit = cirq.Circuit(cirq.Rx(sympy.Symbol('alpha') * 5.0)(bit))
circuit = cirq.Circuit(cirq.rx(sympy.Symbol('alpha') * 5.0)(bit))
inputs = util.convert_to_tensor([circuit])
symbols = tf.convert_to_tensor(['alpha'])
res = tfq_ps_util_ops.tfq_ps_weights_from_symbols(inputs, symbols)
Expand Down
44 changes: 23 additions & 21 deletions tensorflow_quantum/core/serialize/serializer.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def _scalar_extractor(x):
In the future we should likely get rid of this in favor of proper
expression parsing once cirq supports it. See cirq.op_serializer
and cirq's program protobuf for details. This is needed for things
like cirq.Rx('alpha').
like cirq.rx('alpha').
"""
if not isinstance(x, (numbers.Real, sympy.Expr)):
raise TypeError("Invalid input argument for exponent.")
Expand Down Expand Up @@ -111,14 +111,15 @@ def _eigen_gate_serializer(gate_type, serialized_id):
cirq.google.SerializingArg(
serialized_name="exponent",
serialized_type=float,
gate_getter=lambda x: _symbol_extractor(x.exponent)),
op_getter=lambda x: _symbol_extractor(x.gate.exponent)),
cirq.google.SerializingArg(
serialized_name="exponent_scalar",
serialized_type=float,
gate_getter=lambda x: _scalar_extractor(x.exponent)),
cirq.google.SerializingArg(serialized_name="global_shift",
serialized_type=float,
gate_getter=lambda x: float(x._global_shift))
op_getter=lambda x: _scalar_extractor(x.gate.exponent)),
cirq.google.SerializingArg(
serialized_name="global_shift",
serialized_type=float,
op_getter=lambda x: float(x.gate._global_shift))
]
return cirq.google.GateOpSerializer(gate_type=gate_type,
serialized_gate_id=serialized_id,
Expand All @@ -134,7 +135,7 @@ def _scalar_combiner(exponent, global_shift, exponent_scalar):
In the future we should likely get rid of this in favor of proper
expression parsing once cirq supports it. See cirq.op_serializer
and cirq's program protobuf for details. This is needed for things
like cirq.Rx('alpha').
like cirq.rx('alpha').
"""
if exponent_scalar == 1.0:
return gate_type(exponent=exponent, global_shift=global_shift)
Expand All @@ -161,19 +162,19 @@ def _fsim_gate_serializer():
cirq.google.SerializingArg(
serialized_name="theta",
serialized_type=float,
gate_getter=lambda x: _symbol_extractor(x.theta)),
op_getter=lambda x: _symbol_extractor(x.gate.theta)),
cirq.google.SerializingArg(
serialized_name="phi",
serialized_type=float,
gate_getter=lambda x: _symbol_extractor(x.phi)),
op_getter=lambda x: _symbol_extractor(x.gate.phi)),
cirq.google.SerializingArg(
serialized_name="theta_scalar",
serialized_type=float,
gate_getter=lambda x: _scalar_extractor(x.theta)),
op_getter=lambda x: _scalar_extractor(x.gate.theta)),
cirq.google.SerializingArg(
serialized_name="phi_scalar",
serialized_type=float,
gate_getter=lambda x: _scalar_extractor(x.phi)),
op_getter=lambda x: _scalar_extractor(x.gate.phi)),
]
return cirq.google.GateOpSerializer(gate_type=cirq.FSimGate,
serialized_gate_id="FSIM",
Expand Down Expand Up @@ -209,7 +210,7 @@ def _identity_gate_serializer():
"""Make a standard serializer for the single qubit identity."""

def _identity_check(x):
if x.num_qubits() != 1:
if x.gate.num_qubits() != 1:
raise ValueError("Multi-Qubit identity gate not supported."
"Given: {}. To work around this, use "
"cirq.I.on_each instead.".format(str(x)))
Expand All @@ -220,7 +221,7 @@ def _identity_check(x):
args = [
cirq.google.SerializingArg(serialized_name="unused",
serialized_type=bool,
gate_getter=_identity_check)
op_getter=_identity_check)
]
return cirq.google.GateOpSerializer(gate_type=cirq.IdentityGate,
serialized_gate_id="I",
Expand Down Expand Up @@ -250,22 +251,23 @@ def _phased_eigen_gate_serializer(gate_type, serialized_id):
cirq.google.SerializingArg(
serialized_name="phase_exponent",
serialized_type=float,
gate_getter=lambda x: _symbol_extractor(x.phase_exponent)),
op_getter=lambda x: _symbol_extractor(x.gate.phase_exponent)),
cirq.google.SerializingArg(
serialized_name="phase_exponent_scalar",
serialized_type=float,
gate_getter=lambda x: _scalar_extractor(x.phase_exponent)),
op_getter=lambda x: _scalar_extractor(x.gate.phase_exponent)),
cirq.google.SerializingArg(
serialized_name="exponent",
serialized_type=float,
gate_getter=lambda x: _symbol_extractor(x.exponent)),
op_getter=lambda x: _symbol_extractor(x.gate.exponent)),
cirq.google.SerializingArg(
serialized_name="exponent_scalar",
serialized_type=float,
gate_getter=lambda x: _scalar_extractor(x.exponent)),
cirq.google.SerializingArg(serialized_name="global_shift",
serialized_type=float,
gate_getter=lambda x: float(x.global_shift))
op_getter=lambda x: _scalar_extractor(x.gate.exponent)),
cirq.google.SerializingArg(
serialized_name="global_shift",
serialized_type=float,
op_getter=lambda x: float(x.gate.global_shift))
]
return cirq.google.GateOpSerializer(gate_type=gate_type,
serialized_gate_id=serialized_id,
Expand All @@ -282,7 +284,7 @@ def _scalar_combiner(exponent, global_shift, exponent_scalar,
In the future we should likely get rid of this in favor of proper
expression parsing once cirq supports it. See cirq.op_serializer
and cirq's program protobuf for details. This is needed for things
like cirq.Rx('alpha').
like cirq.rx('alpha').
"""
# Do this to help with rounding. it's ugly.
exponent = exponent if exponent_scalar == 1.0 \
Expand Down
12 changes: 6 additions & 6 deletions tensorflow_quantum/core/serialize/serializer_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,15 +346,15 @@ def _get_valid_circuit_proto_pairs():
# RX, RY, RZ with symbolization is tested in special cases as the
# string comparison of the float converted sympy.pi does not happen
# smoothly. See: test_serialize_deserialize_special_case_one_qubit
(cirq.Circuit(cirq.Rx(np.pi)(q0)),
(cirq.Circuit(cirq.rx(np.pi)(q0)),
_build_gate_proto("XP",
['exponent', 'exponent_scalar', 'global_shift'],
[1.0, 1.0, -0.5], ['0_0'])),
(cirq.Circuit(cirq.Ry(np.pi)(q0)),
(cirq.Circuit(cirq.ry(np.pi)(q0)),
_build_gate_proto("YP",
['exponent', 'exponent_scalar', 'global_shift'],
[1.0, 1.0, -0.5], ['0_0'])),
(cirq.Circuit(cirq.Rz(np.pi)(q0)),
(cirq.Circuit(cirq.rz(np.pi)(q0)),
_build_gate_proto("ZP",
['exponent', 'exponent_scalar', 'global_shift'],
[1.0, 1.0, -0.5], ['0_0'])),
Expand Down Expand Up @@ -591,13 +591,13 @@ def test_serialize_deserialize_paulisum_consistency(self, sum_proto_pair):

@parameterized.parameters([
{
'gate': cirq.Rx(3.0 * sympy.Symbol('alpha'))
'gate': cirq.rx(3.0 * sympy.Symbol('alpha'))
},
{
'gate': cirq.Ry(-1.0 * sympy.Symbol('alpha'))
'gate': cirq.ry(-1.0 * sympy.Symbol('alpha'))
},
{
'gate': cirq.Rz(sympy.Symbol('alpha'))
'gate': cirq.rz(sympy.Symbol('alpha'))
},
])
def test_serialize_deserialize_special_case_one_qubit(self, gate):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def test_parse_programs(self):
q = cirq.GridQubit.rect(1, n_qubits)
c = cirq.Circuit()
c.append([
cirq.Rz(coeff[i] * sympy_symbols[i % 2]).on(q[i])
cirq.rz(coeff[i] * sympy_symbols[i % 2]).on(q[i])
for i in range(n_qubits)
])
circuit_batch = [c] * n_programs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _example_circuit_helper(n_qubits, n_programs):
coeff = [1.0, -2.0, 3.0, -4.0, 5.0]
q = cirq.GridQubit.rect(1, n_qubits)
c = cirq.Circuit([
cirq.Rz(coeff[i] * sympy_symbols[i % 2]).on(q[i])
cirq.rz(coeff[i] * sympy_symbols[i % 2]).on(q[i])
for i in range(n_qubits)
])
circuit_batch = [c] * n_programs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ class Expectation(tf.keras.layers.Layer):
... \"""Generate a toy problem on 1 qubit.\"""
... starting_state = [0.123, 0.456, 0.789]
... circuit = cirq.Circuit(
... cirq.Rx(starting_state[0])(bit),
... cirq.Ry(starting_state[1])(bit),
... cirq.Rz(starting_state[2])(bit),
... cirq.Rz(symbols[2])(bit),
... cirq.Ry(symbols[1])(bit),
... cirq.Rx(symbols[0])(bit)
... cirq.rx(starting_state[0])(bit),
... cirq.ry(starting_state[1])(bit),
... cirq.rz(starting_state[2])(bit),
... cirq.rz(symbols[2])(bit),
... cirq.ry(symbols[1])(bit),
... cirq.rx(symbols[0])(bit)
... )
... return circuit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@ def _gen_single_bit_rotation_problem(bit, symbols):
"""Generate a toy problem on 1 qubit."""
starting_state = np.random.uniform(0, 2 * np.pi, 3)
circuit = cirq.Circuit(
cirq.Rx(starting_state[0])(bit),
cirq.Ry(starting_state[1])(bit),
cirq.Rz(starting_state[2])(bit),
cirq.Rz(symbols[2])(bit),
cirq.Ry(symbols[1])(bit),
cirq.Rx(symbols[0])(bit))
cirq.rx(starting_state[0])(bit),
cirq.ry(starting_state[1])(bit),
cirq.rz(starting_state[2])(bit),
cirq.rz(symbols[2])(bit),
cirq.ry(symbols[1])(bit),
cirq.rx(symbols[0])(bit))

return circuit

Expand Down Expand Up @@ -170,7 +170,7 @@ def test_expectation_simple_tf_train(self):
"""
bit = cirq.GridQubit(0, 0)
circuit = \
cirq.Circuit(cirq.Rx(sympy.Symbol('theta'))(bit))
cirq.Circuit(cirq.rx(sympy.Symbol('theta'))(bit))
op = cirq.Z(bit)
layer = expectation.Expectation()
optimizer = tf.optimizers.Adam(learning_rate=0.05)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ class SampledExpectation(tf.keras.layers.Layer):
... \"""Generate a toy problem on 1 qubit.\"""
... starting_state = [0.123, 0.456, 0.789]
... circuit = cirq.Circuit(
... cirq.Rx(starting_state[0])(bit),
... cirq.Ry(starting_state[1])(bit),
... cirq.Rz(starting_state[2])(bit),
... cirq.Rz(symbols[2])(bit),
... cirq.Ry(symbols[1])(bit),
... cirq.Rx(symbols[0])(bit)
... cirq.rx(starting_state[0])(bit),
... cirq.ry(starting_state[1])(bit),
... cirq.rz(starting_state[2])(bit),
... cirq.rz(symbols[2])(bit),
... cirq.ry(symbols[1])(bit),
... cirq.rx(symbols[0])(bit)
... )
... return circuit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,12 @@ def _gen_single_bit_rotation_problem(bit, symbols):
"""Generate a toy problem on 1 qubit."""
starting_state = np.random.uniform(0, 2 * np.pi, 3)
circuit = cirq.Circuit(
cirq.Rx(starting_state[0])(bit),
cirq.Ry(starting_state[1])(bit),
cirq.Rz(starting_state[2])(bit),
cirq.Rz(symbols[2])(bit),
cirq.Ry(symbols[1])(bit),
cirq.Rx(symbols[0])(bit))
cirq.rx(starting_state[0])(bit),
cirq.ry(starting_state[1])(bit),
cirq.rz(starting_state[2])(bit),
cirq.rz(symbols[2])(bit),
cirq.ry(symbols[1])(bit),
cirq.rx(symbols[0])(bit))

return circuit

Expand Down Expand Up @@ -227,7 +227,7 @@ def test_static_cases(self):
def test_sampled_expectation_simple_tf_train(self):
"""Train a layer using standard tf (not keras)."""
bit = cirq.GridQubit(0, 0)
circuit = cirq.Circuit(cirq.Rx(sympy.Symbol('theta'))(bit))
circuit = cirq.Circuit(cirq.rx(sympy.Symbol('theta'))(bit))
layer = sampled_expectation.SampledExpectation()
optimizer = tf.optimizers.Adam(learning_rate=0.05)
for _ in range(10):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ def test_controlled_pqc_simple_learn(self, backend, repetitions):
on many backends."""
bit = cirq.GridQubit(0, 0)
circuit = \
cirq.Circuit(cirq.Rx(sympy.Symbol('theta'))(bit))
cirq.Circuit(cirq.rx(sympy.Symbol('theta'))(bit))

inputs = tf.keras.Input(shape=(1,), dtype=tf.dtypes.float32)
quantum_datum = tf.keras.Input(shape=(), dtype=tf.dtypes.string)
Expand Down
20 changes: 10 additions & 10 deletions tensorflow_quantum/python/layers/high_level/pqc.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@ class PQC(tf.keras.layers.Layer):
>>> q = cirq.GridQubit(0, 0)
>>> (a, b, c) = sympy.symbols("a b c")
>>> circuit = cirq.Circuit(
... cirq.Rz(a)(q),
... cirq.Rx(b)(q),
... cirq.Rz(c)(q),
... cirq.Rx(-b)(q),
... cirq.Rz(-a)(q)
... cirq.rz(a)(q),
... cirq.rx(b)(q),
... cirq.rz(c)(q),
... cirq.rx(-b)(q),
... cirq.rz(-a)(q)
... )
Expand Down Expand Up @@ -101,11 +101,11 @@ class PQC(tf.keras.layers.Layer):
>>> q = cirq.GridQubit(0, 0)
>>> (a, b, c) = sympy.symbols("a b c")
>>> circuit = cirq.Circuit(
... cirq.Rz(a)(q),
... cirq.Rx(b)(q),
... cirq.Rz(c)(q),
... cirq.Rx(-b)(q),
... cirq.Rz(-a)(q)
... cirq.rz(a)(q),
... cirq.rx(b)(q),
... cirq.rz(c)(q),
... cirq.rx(-b)(q),
... cirq.rz(-a)(q)
... )
>>> measurement = [cirq.X(q), cirq.Y(q), cirq.Z(q)]
>>> outputs = tfq.layers.PQC(
Expand Down
Loading

0 comments on commit 41be90d

Please sign in to comment.