def test_controlled_pqc_instantiate(self): """Basic creation test.""" symbol = sympy.Symbol('alpha') bit = cirq.GridQubit(0, 0) learnable_flip = cirq.Circuit(cirq.X(bit)**symbol) controlled_pqc.ControlledPQC(learnable_flip, cirq.Z(bit)) controlled_pqc.ControlledPQC(learnable_flip, cirq.Z(bit), repetitions=500)
def test_controlled_pqc_model_circuit_error(self): """Test that invalid circuits error properly.""" bit = cirq.GridQubit(0, 0) no_symbols = cirq.Circuit(cirq.X(bit)) with self.assertRaisesRegex(TypeError, expected_regex="cirq.Circuit"): controlled_pqc.ControlledPQC('junk', cirq.Z(bit)) with self.assertRaisesRegex(ValueError, expected_regex="no sympy.Symbols"): controlled_pqc.ControlledPQC(no_symbols, cirq.Z(bit))
def test_controlled_pqc_operators_error(self): """Test that invalid operators error properly.""" symbol = sympy.Symbol('alpha') bit = cirq.GridQubit(0, 0) learnable_flip = cirq.Circuit(cirq.X(bit)**symbol) with self.assertRaisesRegex( TypeError, expected_regex="cirq.PauliSum or cirq.PauliString"): controlled_pqc.ControlledPQC(learnable_flip, 'junk') with self.assertRaisesRegex(TypeError, expected_regex="Each element"): controlled_pqc.ControlledPQC(learnable_flip, [[cirq.Z(bit)]]) with self.assertRaisesRegex(TypeError, expected_regex="Each element"): controlled_pqc.ControlledPQC(learnable_flip, [cirq.Z(bit), 'bad'])
def test_controlled_pqc_simple_learn(self, backend, repetitions): """Test a simple learning scenario using analytic and sample expectation on many backends.""" bit = cirq.GridQubit(0, 0) circuit = \ 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) l1 = tf.keras.layers.Dense(10)(inputs) l2 = tf.keras.layers.Dense(1)(l1) outputs = controlled_pqc.ControlledPQC(circuit, cirq.Z(bit), repetitions=repetitions, backend=backend)( [quantum_datum, l2]) model = tf.keras.Model(inputs=[quantum_datum, inputs], outputs=outputs) data_in = np.array([[1], [0]], dtype=np.float32) data_out = np.array([[1], [-1]], dtype=np.float32) model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=0.05), loss=tf.keras.losses.mean_squared_error) data_circuits = util.convert_to_tensor( [cirq.Circuit(cirq.X(bit)), cirq.Circuit()]) history = model.fit(x=[data_circuits, data_in], y=data_out, epochs=30) self.assertAllClose(history.history['loss'][-1], 0, atol=1e-1)
def test_controlled_pqc_repetitions_error(self): """Test that invalid repetitions error properly.""" symbol = sympy.Symbol('alpha') bit = cirq.GridQubit(0, 0) learnable_flip = cirq.Circuit(cirq.X(bit)**symbol) with self.assertRaisesRegex(ValueError, expected_regex="greater than zero."): controlled_pqc.ControlledPQC(learnable_flip, cirq.Z(bit), repetitions=-100) with self.assertRaisesRegex(TypeError, expected_regex="positive integer value"): controlled_pqc.ControlledPQC(learnable_flip, cirq.Z(bit), repetitions='junk')
def test_controlled_pqc_noisy_error(self): """Ensure error refers to alternate layer.""" symbol = sympy.Symbol('alpha') qubit = cirq.GridQubit(0, 0) learnable_flip = cirq.Circuit(cirq.X(qubit)**symbol) with self.assertRaisesRegex( ValueError, expected_regex='tfq.layers.NoisyControlledPQC'): controlled_pqc.ControlledPQC(learnable_flip, cirq.Z(qubit), backend='noisy')
def test_controlled_pqc_symbols_property(self): """Test that the `symbols` property returns the symbols.""" 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) layer = controlled_pqc.ControlledPQC(test_circuit, cirq.Z(bit)) self.assertEqual(layer.symbols, [a, b, c, d])
def test_controlled_pqc_backend_error(self): """Test that invalid backends error properly.""" symbol = sympy.Symbol('alpha') bit = cirq.GridQubit(0, 0) learnable_flip = cirq.Circuit(cirq.X(bit)**symbol) class MyExpectation(cirq.sim.simulator.SimulatesExpectationValues): """My expectation values simulator.""" def simulate_expectation_values_sweep(self): """do nothing.""" return class MySample(cirq.Sampler): """My state simulator.""" def run_sweep(self): """do nothing.""" return with self.assertRaisesRegex( TypeError, expected_regex="cirq.sim.simulator.SimulatesExpectation"): controlled_pqc.ControlledPQC(learnable_flip, cirq.Z(bit), backend='junk') with self.assertRaisesRegex( TypeError, expected_regex="cirq.sim.simulator.SimulatesExpectation"): controlled_pqc.ControlledPQC(learnable_flip, cirq.Z(bit), repetitions=None, backend=MySample) with self.assertRaisesRegex(TypeError, expected_regex="cirq.Sampler"): controlled_pqc.ControlledPQC(learnable_flip, cirq.Z(bit), repetitions=500, backend=MyExpectation)