Skip to content

Commit

Permalink
Added Symbol values function to PQC (tensorflow#177)
Browse files Browse the repository at this point in the history
* Added symbol_values function to PQC.

* strange format.

Co-authored-by: Michael Broughton <[email protected]>
  • Loading branch information
MichaelBroughton and MichaelBroughton authored Mar 30, 2020
1 parent 37d7d0d commit 46aa11b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions tensorflow_quantum/python/layers/high_level/pqc.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,6 +270,15 @@ def symbols(self):
"""
return [sympy.Symbol(x) for x in self._symbols_list]

def symbol_values(self):
"""Returns a Python `dict` containing symbol name, value pairs.
Returns:
Python `dict` with `str` keys and `float` values representing
the current symbol values.
"""
return dict(zip(self.symbols, self.get_weights()[0]))

def build(self, input_shape):
"""Keras build function."""
super().build(input_shape)
Expand Down
16 changes: 16 additions & 0 deletions tensorflow_quantum/python/layers/high_level/pqc_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,22 @@ def test_pqc_symbols_property(self):
layer = pqc.PQC(test_circuit, cirq.Z(bit))
self.assertEqual(layer.symbols, [a, b, c, d])

def test_pqc_symbol_values(self):
"""Test that PQC symbol_values returns the correct key value pairs."""
c, b, a, d = sympy.symbols('c b a d')
bit = cirq.GridQubit(0, 0)
test_circuit = cirq.Circuit(
cirq.H(bit)**a,
cirq.Z(bit)**b,
cirq.X(bit)**d,
cirq.Y(bit)**c)
init_vals = [1, 2, 3, 4]
layer = pqc.PQC(test_circuit,
cirq.Z(bit),
initializer=tf.constant_initializer(init_vals))
expected_vals = dict(zip([a, b, c, d], init_vals))
self.assertAllClose(layer.symbol_values(), expected_vals)

@parameterized.parameters(
list(
util.kwargs_cartesian_product(backend=[None, cirq.Simulator()],
Expand Down

0 comments on commit 46aa11b

Please sign in to comment.