Ejemplo n.º 1
0
    def test_simulate_state_output_padding(self, all_n_qubits):
        """If a tfq_simulate op is asked to simulate states given circuits
        acting on different numbers of qubits, the op should return a tensor
        padded with zeros up to the size of the largest circuit. The padding
        should be physically correct, such that samples taken from the padded
        states still match samples taken from the original circuit. """
        circuit_batch = []
        for n_qubits in all_n_qubits:
            qubits = cirq.GridQubit.rect(1, n_qubits)
            circuit_batch += util.random_circuit_resolver_batch(qubits, 1)[0]

        tfq_results = tfq_simulate_ops.tfq_simulate_state(
            util.convert_to_tensor(circuit_batch), [],
            [[]] * len(circuit_batch))

        # Don't use batch_util here to enforce consistent padding everywhere
        # without extra tests.
        sim = cirq.Simulator()
        manual_padded_results = []
        for circuit in circuit_batch:
            result = sim.simulate(circuit)
            wf = result.final_state
            blank_state = np.ones(
                (2**max(all_n_qubits)), dtype=np.complex64) * -2
            blank_state[:wf.shape[0]] = wf
            manual_padded_results.append(blank_state)

        self.assertAllClose(tfq_results, manual_padded_results)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
0
    def test_simulate_state_inputs(self):
        """Make sure the state 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])

        with self.assertRaisesRegex(tf.errors.InvalidArgumentError,
                                    'programs must be rank 1'):
            # programs tensor has the wrong shape.
            tfq_simulate_ops.tfq_simulate_state(
                util.convert_to_tensor([circuit_batch]), symbol_names,
                symbol_values_array)

        with self.assertRaisesRegex(tf.errors.InvalidArgumentError,
                                    'symbol_names must be rank 1'):
            # symbol_names tensor has the wrong shape.
            tfq_simulate_ops.tfq_simulate_state(
                util.convert_to_tensor(circuit_batch),
                np.array([symbol_names]), symbol_values_array)

        with self.assertRaisesRegex(tf.errors.InvalidArgumentError,
                                    'symbol_values must be rank 2'):
            # symbol_values tensor has the wrong shape.
            tfq_simulate_ops.tfq_simulate_state(
                util.convert_to_tensor(circuit_batch), symbol_names,
                np.array([symbol_values_array]))

        with self.assertRaisesRegex(tf.errors.InvalidArgumentError,
                                    'symbol_values must be rank 2'):
            # symbol_values tensor has the wrong shape 2.
            tfq_simulate_ops.tfq_simulate_state(
                util.convert_to_tensor(circuit_batch), symbol_names,
                symbol_values_array[0])

        with self.assertRaisesRegex(tf.errors.InvalidArgumentError,
                                    'Unparseable proto'):
            # programs tensor has the right type, but invalid value.
            tfq_simulate_ops.tfq_simulate_state(['junk'] * batch_size,
                                                symbol_names,
                                                symbol_values_array)

        with self.assertRaisesRegex(tf.errors.InvalidArgumentError,
                                    'Could not find symbol in parameter map'):
            # symbol_names tensor has the right type, but invalid value.
            tfq_simulate_ops.tfq_simulate_state(
                util.convert_to_tensor(circuit_batch), ['junk'],
                symbol_values_array)

        with self.assertRaisesRegex(TypeError, 'Cannot convert'):
            # programs tensor has the wrong type.
            tfq_simulate_ops.tfq_simulate_state([1] * batch_size, symbol_names,
                                                symbol_values_array)

        with self.assertRaisesRegex(TypeError, 'Cannot convert'):
            # symbol_names tensor has the wrong type.
            tfq_simulate_ops.tfq_simulate_state(
                util.convert_to_tensor(circuit_batch), [1],
                symbol_values_array)

        with self.assertRaisesRegex(tf.errors.UnimplementedError, ''):
            # symbol_values tensor has the wrong type.
            tfq_simulate_ops.tfq_simulate_state(
                util.convert_to_tensor(circuit_batch), symbol_names,
                [['junk']] * batch_size)

        with self.assertRaisesRegex(TypeError, 'missing'):
            # too few tensors.
            # pylint: disable=no-value-for-parameter
            tfq_simulate_ops.tfq_simulate_state(
                util.convert_to_tensor(circuit_batch), symbol_names)
            # pylint: enable=no-value-for-parameter

        # TODO (mbbrough): determine if we should allow extra arguments ?
        with self.assertRaisesRegex(TypeError, 'positional arguments'):
            # pylint: disable=too-many-function-args
            tfq_simulate_ops.tfq_simulate_state(
                util.convert_to_tensor(circuit_batch), symbol_names,
                symbol_values_array, [])
Ejemplo n.º 4
0
 def _simulate_circuit(self, circuit, params):
     # TODO: implement backend switch
     return tfq_simulate_ops.tfq_simulate_state(
         [str(serialize_circuit(circuit))] * params.batch_size, ["None"],
         [[0]] * params.batch_size)