def test_symbol_values_type(self, symbol_type): """Tests all three ops for the different types. """ qubit = cirq.GridQubit(0, 0) circuits = util.convert_to_tensor([cirq.Circuit(cirq.H(qubit))]) symbol_names = ['symbol'] symbol_values = tf.convert_to_tensor([[1]], dtype=symbol_type) pauli_sums = util.random_pauli_sums([qubit], 3, 1) pauli_sums = util.convert_to_tensor([[x] for x in pauli_sums]) result = tfq_simulate_ops.tfq_simulate_state(circuits, symbol_names, symbol_values) self.assertDTypeEqual(result, np.complex64) result = tfq_simulate_ops.tfq_simulate_expectation( circuits, symbol_names, symbol_values, pauli_sums) self.assertDTypeEqual(result, np.float32) result = tfq_simulate_ops.tfq_simulate_samples(circuits, symbol_names, symbol_values, [100]) self.assertDTypeEqual(result, np.int8)
def test_simulate_expectation_inputs(self): """Make sure the the expectation op fails gracefully on bad inputs.""" n_qubits = 5 batch_size = 5 symbol_names = ['alpha'] qubits = cirq.GridQubit.rect(1, n_qubits) circuit_batch, resolver_batch = \ util.random_symbol_circuit_resolver_batch( qubits, symbol_names, batch_size) symbol_values_array = np.array( [[resolver[symbol] for symbol in symbol_names] for resolver in resolver_batch]) pauli_sums = util.random_pauli_sums(qubits, 3, batch_size) with self.assertRaisesRegex(tf.errors.InvalidArgumentError, 'programs must be rank 1'): # Circuit tensor has too many dimensions. tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor([circuit_batch]), symbol_names, symbol_values_array, util.convert_to_tensor([[x] for x in pauli_sums])) with self.assertRaisesRegex(tf.errors.InvalidArgumentError, 'symbol_names must be rank 1.'): # symbol_names tensor has too many dimensions. tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor(circuit_batch), np.array([symbol_names]), symbol_values_array, util.convert_to_tensor([[x] for x in pauli_sums])) with self.assertRaisesRegex(tf.errors.InvalidArgumentError, 'symbol_values must be rank 2.'): # symbol_values_array tensor has too many dimensions. tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor(circuit_batch), symbol_names, np.array([symbol_values_array]), util.convert_to_tensor([[x] for x in pauli_sums])) with self.assertRaisesRegex(tf.errors.InvalidArgumentError, 'symbol_values must be rank 2.'): # symbol_values_array tensor has too few dimensions. tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor(circuit_batch), symbol_names, symbol_values_array[0], util.convert_to_tensor([[x] for x in pauli_sums])) with self.assertRaisesRegex(tf.errors.InvalidArgumentError, 'pauli_sums must be rank 2.'): # pauli_sums tensor has too few dimensions. tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor(circuit_batch), symbol_names, symbol_values_array, util.convert_to_tensor([x for x in pauli_sums])) with self.assertRaisesRegex(tf.errors.InvalidArgumentError, 'pauli_sums must be rank 2.'): # pauli_sums tensor has too many dimensions. tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor(circuit_batch), symbol_names, symbol_values_array, util.convert_to_tensor([[[x]] for x in pauli_sums])) with self.assertRaisesRegex(tf.errors.InvalidArgumentError, 'Unparseable proto'): # circuit tensor has the right type but invalid values. tfq_simulate_ops.tfq_simulate_expectation( ['junk'] * batch_size, symbol_names, symbol_values_array, util.convert_to_tensor([[x] for x in pauli_sums])) with self.assertRaisesRegex(tf.errors.InvalidArgumentError, 'Could not find symbol in parameter map'): # symbol_names tensor has the right type but invalid values. tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor(circuit_batch), ['junk'], symbol_values_array, util.convert_to_tensor([[x] for x in pauli_sums])) with self.assertRaisesRegex(tf.errors.InvalidArgumentError, 'qubits not found in circuit'): # pauli_sums tensor has the right type but invalid values. new_qubits = [cirq.GridQubit(5, 5), cirq.GridQubit(9, 9)] new_pauli_sums = util.random_pauli_sums(new_qubits, 2, batch_size) tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor(circuit_batch), symbol_names, symbol_values_array, util.convert_to_tensor([[x] for x in new_pauli_sums])) with self.assertRaisesRegex(tf.errors.InvalidArgumentError, 'Unparseable proto'): # pauli_sums tensor has the right type but invalid values 2. tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor(circuit_batch), symbol_names, symbol_values_array, [['junk']] * batch_size) with self.assertRaisesRegex(TypeError, 'Cannot convert'): # circuits tensor has the wrong type. tfq_simulate_ops.tfq_simulate_expectation( [1.0] * batch_size, symbol_names, symbol_values_array, util.convert_to_tensor([[x] for x in pauli_sums])) with self.assertRaisesRegex(TypeError, 'Cannot convert'): # symbol_names tensor has the wrong type. tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor(circuit_batch), [0.1234], symbol_values_array, util.convert_to_tensor([[x] for x in pauli_sums])) with self.assertRaisesRegex(tf.errors.UnimplementedError, ''): # symbol_values tensor has the wrong type. tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor(circuit_batch), symbol_names, [['junk']] * batch_size, util.convert_to_tensor([[x] for x in pauli_sums])) with self.assertRaisesRegex(TypeError, 'Cannot convert'): # pauli_sums tensor has the wrong type. tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor(circuit_batch), symbol_names, symbol_values_array, [[1.0]] * batch_size) with self.assertRaisesRegex(TypeError, 'missing'): # we are missing an argument. # pylint: disable=no-value-for-parameter tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor(circuit_batch), symbol_names, symbol_values_array) # pylint: enable=no-value-for-parameter with self.assertRaisesRegex(TypeError, 'positional arguments'): # pylint: disable=too-many-function-args tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor(circuit_batch), symbol_names, symbol_values_array, util.convert_to_tensor([[x] for x in pauli_sums]), []) with self.assertRaisesRegex(tf.errors.InvalidArgumentError, expected_regex='do not match'): # wrong op size. tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor([cirq.Circuit()]), symbol_names, symbol_values_array.astype(np.float64), util.convert_to_tensor([[x] for x in pauli_sums])) res = tfq_simulate_ops.tfq_simulate_expectation( util.convert_to_tensor([cirq.Circuit() for _ in pauli_sums]), symbol_names, symbol_values_array.astype(np.float64), util.convert_to_tensor([[x] for x in pauli_sums])) self.assertDTypeEqual(res, np.float32)