Skip to content

Commit

Permalink
format.
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelBroughton committed Apr 28, 2021
1 parent a476c98 commit 7d29a44
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 51 deletions.
9 changes: 3 additions & 6 deletions tensorflow_quantum/python/layers/high_level/noisy_pqc.py
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ def __init__(
if not isinstance(sample_based, bool):
raise TypeError("sampled_based must be either True or False."
" received: {}".format(type(sample_based)))

if not sample_based:
self._executor = differentiator.generate_differentiable_op(
sampled_op=noisy_expectation_op.expectation)
Expand Down Expand Up @@ -288,11 +288,8 @@ def call(self, inputs):
tiled_up_parameters = tf.tile([self.parameters], [circuit_batch_dim, 1])
tiled_up_operators = tf.tile(self._operators, [circuit_batch_dim, 1])


tiled_up_repetitions = tf.tile(self._repetitions,
[circuit_batch_dim, 1])
return self._executor(model_appended,
self._symbols,
tiled_up_parameters,
tiled_up_operators,
return self._executor(model_appended, self._symbols,
tiled_up_parameters, tiled_up_operators,
tiled_up_repetitions)
128 changes: 83 additions & 45 deletions tensorflow_quantum/python/layers/high_level/noisy_pqc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,10 @@ def test_noisy_pqc_instantiate(self):
symbol = sympy.Symbol('alpha')
qubit = cirq.GridQubit(0, 0)
learnable_flip = cirq.Circuit(cirq.X(qubit)**symbol)
noisy_pqc.NoisyPQC(
learnable_flip, cirq.Z(qubit), repetitions=1000, sample_based=False)
noisy_pqc.NoisyPQC(learnable_flip,
cirq.Z(qubit),
repetitions=1000,
sample_based=False)

def test_noisy_pqc_model_circuit_error(self):
"""Test that invalid circuits error properly."""
Expand All @@ -42,14 +44,18 @@ def test_noisy_pqc_model_circuit_error(self):
with self.assertRaisesRegex(
TypeError,
expected_regex="model_circuit must be a cirq.Circuit"):
noisy_pqc.NoisyPQC(
'junk', cirq.Z(qubit), repetitions=1000, sample_based=False)
noisy_pqc.NoisyPQC('junk',
cirq.Z(qubit),
repetitions=1000,
sample_based=False)

with self.assertRaisesRegex(
ValueError,
expected_regex="model_circuit has no sympy.Symbols"):
noisy_pqc.NoisyPQC(
no_symbols, cirq.Z(qubit), repetitions=1000, sample_based=False)
noisy_pqc.NoisyPQC(no_symbols,
cirq.Z(qubit),
repetitions=1000,
sample_based=False)

def test_noisy_pqc_operators_error(self):
"""Test that invalid operators error properly."""
Expand All @@ -59,14 +65,20 @@ def test_noisy_pqc_operators_error(self):

with self.assertRaisesRegex(
TypeError, expected_regex="cirq.PauliSum or cirq.PauliString"):
noisy_pqc.NoisyPQC(
learnable_flip, 'junk', repetitions=1000, sample_based=False)
noisy_pqc.NoisyPQC(learnable_flip,
'junk',
repetitions=1000,
sample_based=False)

with self.assertRaisesRegex(TypeError, expected_regex="Each element"):
noisy_pqc.NoisyPQC(learnable_flip, [[cirq.Z(qubit)]], repetitions=1000, sample_based=False)
noisy_pqc.NoisyPQC(learnable_flip, [[cirq.Z(qubit)]],
repetitions=1000,
sample_based=False)

with self.assertRaisesRegex(TypeError, expected_regex="Each element"):
noisy_pqc.NoisyPQC(learnable_flip, [cirq.Z(qubit), 'bad'], repetitions=1000, sample_based=False)
noisy_pqc.NoisyPQC(learnable_flip, [cirq.Z(qubit), 'bad'],
repetitions=1000,
sample_based=False)

def test_noisy_pqc_repetitions_error(self):
"""Test that invalid repetitions error properly."""
Expand All @@ -76,20 +88,30 @@ def test_noisy_pqc_repetitions_error(self):

with self.assertRaisesRegex(TypeError,
expected_regex="positive integer value"):
noisy_pqc.NoisyPQC(learnable_flip, cirq.Z(qubit), repetitions='junk', sample_based=False)
noisy_pqc.NoisyPQC(learnable_flip,
cirq.Z(qubit),
repetitions='junk',
sample_based=False)

with self.assertRaisesRegex(ValueError,
expected_regex="greater than zero."):
noisy_pqc.NoisyPQC(learnable_flip, cirq.Z(qubit), repetitions=-100, sample_based=False)
noisy_pqc.NoisyPQC(learnable_flip,
cirq.Z(qubit),
repetitions=-100,
sample_based=False)

with self.assertRaisesRegex(ValueError,
expected_regex="greater than zero."):
noisy_pqc.NoisyPQC(learnable_flip, cirq.Z(qubit), repetitions=0, sample_based=False)
noisy_pqc.NoisyPQC(learnable_flip,
cirq.Z(qubit),
repetitions=0,
sample_based=False)

with self.assertRaisesRegex(ValueError,
expected_regex="must be provided"):
noisy_pqc.NoisyPQC(learnable_flip, cirq.Z(qubit), sample_based=False)

noisy_pqc.NoisyPQC(learnable_flip,
cirq.Z(qubit),
sample_based=False)

def test_noisy_pqc_sample_based_error(self):
"""Test that invalid sampled_based values error properly."""
Expand All @@ -98,12 +120,15 @@ def test_noisy_pqc_sample_based_error(self):
learnable_flip = cirq.Circuit(cirq.X(qubit)**symbol)

with self.assertRaisesRegex(TypeError, expected_regex="True or False"):
noisy_pqc.NoisyPQC(learnable_flip, cirq.Z(qubit), repetitions=10, sample_based='junk')
noisy_pqc.NoisyPQC(learnable_flip,
cirq.Z(qubit),
repetitions=10,
sample_based='junk')

with self.assertRaisesRegex(ValueError, expected_regex="specify use_sampled=False"):
with self.assertRaisesRegex(ValueError,
expected_regex="specify use_sampled=False"):
noisy_pqc.NoisyPQC(learnable_flip, cirq.Z(qubit), repetitions=10)


def test_noisy_pqc_initializer(self):
"""Test action of initializer."""
(a, b, c) = sympy.symbols("a b c")
Expand All @@ -113,12 +138,15 @@ def test_noisy_pqc_initializer(self):
cirq.Y(qubit)**b,
cirq.Z(qubit)**c])
mpqc_zeros = noisy_pqc.NoisyPQC(three_parameters,
cirq.Z(qubit),
repetitions=100,
sample_based=False,
initializer='zeros')
mpqc_ones = noisy_pqc.NoisyPQC(
three_parameters, cirq.Z(qubit), initializer='ones', repetitions=100, sample_based=False)
cirq.Z(qubit),
repetitions=100,
sample_based=False,
initializer='zeros')
mpqc_ones = noisy_pqc.NoisyPQC(three_parameters,
cirq.Z(qubit),
initializer='ones',
repetitions=100,
sample_based=False)
self.assertAllEqual([[0, 0, 0]], mpqc_zeros.get_weights())
self.assertAllEqual([[1, 1, 1]], mpqc_ones.get_weights())

Expand All @@ -130,10 +158,15 @@ def test_noisy_pqc_regularizer(self):
[cirq.X(qubit)**a,
cirq.Y(qubit)**b,
cirq.Z(qubit)**c])
mpqc = noisy_pqc.NoisyPQC(
three_parameters, cirq.Z(qubit), repetitions=100, sample_based=False)
mpqc_r = noisy_pqc.NoisyPQC(
three_parameters, cirq.Z(qubit), regularizer='l2', repetitions=100, sample_based=False)
mpqc = noisy_pqc.NoisyPQC(three_parameters,
cirq.Z(qubit),
repetitions=100,
sample_based=False)
mpqc_r = noisy_pqc.NoisyPQC(three_parameters,
cirq.Z(qubit),
regularizer='l2',
repetitions=100,
sample_based=False)
self.assertEqual(0, len(mpqc.losses))
self.assertEqual(1, len(mpqc_r.losses))

Expand All @@ -147,10 +180,10 @@ def test_noisy_pqc_constraint(self):
cirq.Y(qubit)**b,
cirq.Z(qubit)**c])
mpqc = noisy_pqc.NoisyPQC(three_parameters,
cirq.Z(qubit),
repetitions=100,
sample_based=False,
constraint=my_constraint)
cirq.Z(qubit),
repetitions=100,
sample_based=False,
constraint=my_constraint)
self.assertEqual(my_constraint, mpqc.parameters.constraint)

def test_noisy_pqc_symbols_property(self):
Expand All @@ -162,8 +195,10 @@ def test_noisy_pqc_symbols_property(self):
cirq.Z(bit)**b,
cirq.X(bit)**d,
cirq.Y(bit)**c)
layer = noisy_pqc.NoisyPQC(
test_circuit, cirq.Z(bit), repetitions=100, sample_based=False)
layer = noisy_pqc.NoisyPQC(test_circuit,
cirq.Z(bit),
repetitions=100,
sample_based=False)
self.assertEqual(layer.symbols, [a, b, c, d])

def test_noisy_pqc_symbol_values(self):
Expand All @@ -176,11 +211,12 @@ def test_noisy_pqc_symbol_values(self):
cirq.X(bit)**d,
cirq.Y(bit)**c)
init_vals = [1, 2, 3, 4]
layer = noisy_pqc.NoisyPQC(test_circuit,
cirq.Z(bit),
repetitions=1000,
sample_based=False,
initializer=tf.constant_initializer(init_vals))
layer = noisy_pqc.NoisyPQC(
test_circuit,
cirq.Z(bit),
repetitions=1000,
sample_based=False,
initializer=tf.constant_initializer(init_vals))
expected_vals = dict(zip([a, b, c, d], init_vals))
self.assertAllClose(layer.symbol_values(), expected_vals)

Expand All @@ -189,15 +225,17 @@ def test_noisy_pqc_simple_learn(self, sample_based):
"""Test a simple learning scenario using analytic and sample expectation
on many backends."""
qubit = cirq.GridQubit(0, 0)
circuit = cirq.Circuit(cirq.X(qubit)**sympy.Symbol('bit'),
circuit = cirq.Circuit(
cirq.X(qubit)**sympy.Symbol('bit'),
cirq.depolarize(0.01)(qubit))

quantum_datum = tf.keras.Input(shape=(), dtype=tf.dtypes.string)
mpqc = noisy_pqc.NoisyPQC(circuit,
cirq.Z(qubit),
repetitions=5000,
sample_based=sample_based,
initializer=tf.keras.initializers.Constant(value=0.5))
mpqc = noisy_pqc.NoisyPQC(
circuit,
cirq.Z(qubit),
repetitions=5000,
sample_based=sample_based,
initializer=tf.keras.initializers.Constant(value=0.5))
outputs = mpqc(quantum_datum)
model = tf.keras.Model(inputs=quantum_datum, outputs=outputs)

Expand Down

0 comments on commit 7d29a44

Please sign in to comment.