Esempio n. 1
0
    def test_serialize_deserialize_circuit_consistency(self, circ_proto_pair):
        """Ensure that serializing followed by deserializing works."""

        # String casting is done here to round floating point values.
        # cirq.testing.assert_same_circuits will call  break and think
        # cirq.Z^0.30000001 is different from cirq.Z^0.3
        self.assertProtoEquals(
            serializer.serialize_circuit(
                serializer.deserialize_circuit(circ_proto_pair[1])),
            circ_proto_pair[1])
        self.assertEqual(
            serializer.deserialize_circuit(
                serializer.serialize_circuit(circ_proto_pair[0])),
            circ_proto_pair[0])
Esempio n. 2
0
    def test_deserialize_circuit_valid(self, circ_proto_pair):
        """Test deserialization of protos in tfq_gate_set."""

        # String casting is done here to round floating point values.
        # cirq.testing.assert_same_circuits will call  break and think
        # cirq.Z^0.30000001 is different from cirq.Z^0.3
        self.assertEqual(circ_proto_pair[0],
                         serializer.deserialize_circuit(circ_proto_pair[1]))
Esempio n. 3
0
    def test_serialize_deserialize_special_case_one_qubit(self, gate):
        """Check output state equality."""
        q0 = cirq.GridQubit(0, 0)
        c = cirq.Circuit(gate(q0))

        c = c._resolve_parameters_(cirq.ParamResolver({"alpha": 0.1234567}))
        before = c.unitary()
        c2 = serializer.deserialize_circuit(serializer.serialize_circuit(c))
        after = c2.unitary()
        self.assertAllClose(before, after)
Esempio n. 4
0
def _parse_single(item):
    try:
        if b'tfq_gate_set' in item:
            # Return a circuit parsing
            obj = cirq.google.api.v2.program_pb2.Program()
            obj.ParseFromString(item)
            out = serializer.deserialize_circuit(obj)
            return out

        # Return a PauliSum parsing.
        obj = pauli_sum_pb2.PauliSum()
        obj.ParseFromString(item)
        out = serializer.deserialize_paulisum(obj)
        return out
    except Exception:
        raise TypeError('Error decoding item: ' + str(item))
Esempio n. 5
0
def _batch_deserialize_helper(programs, symbol_names, symbol_values):
    """Helper function that converts tensors to cirq constructs.

     Converts the string representation of the circuits in `programs`
     to `cirq.Circuit` objects and produces a corresponding
     `cirq.ParamResolver` constructed using `symbol_names` and `symbol_values`.

    Args:
        programs: `tf.Tensor` of strings with shape [batch_size] containing
            the string representations of the circuits to be executed.
        symbol_names: `tf.Tensor` of strings with shape [n_params], which
            is used to specify the order in which the values in
            `symbol_values` should be placed inside of the circuits in
            `programs`.
        symbol_values: `tf.Tensor` of real numbers with shape
            [batch_size, n_params] specifying parameter values to resolve
            into the circuits specified by programs, following the ordering
            dictated by `symbol_names`.

    Returns:
        `tuple` containing a `list` of `cirq.Circuit`s constructed from programs
        and a `list` of `cirq.ParamResolver`s.
    """
    de_ser_symbol_names = [x.decode('UTF-8') for x in symbol_names.numpy()]
    de_ser_programs = []
    resolvers = []
    # TODO(zaqqwerty): investigate parallelization of this loop
    for program, values in zip(programs, symbol_values):
        program = program.numpy()
        values = values.numpy().astype(float)

        circuit_proto = program_pb2.Program()
        circuit_proto.ParseFromString(program)

        circuit = serializer.deserialize_circuit(circuit_proto)

        resolver = cirq.study.resolver.ParamResolver(
            dict(zip(de_ser_symbol_names, values)))
        de_ser_programs.append(circuit)
        resolvers.append(resolver)
    return de_ser_programs, resolvers
Esempio n. 6
0
 def test_deserialize_circuit_wrong_type(self, inp):
     """Attempt to deserialize invalid objects types."""
     with self.assertRaises(TypeError):
         serializer.deserialize_circuit(input)
Esempio n. 7
0
 def test_serialize_circuit_valid_number_types(self, gate_with_param):
     """Tests number datatype support by our serializer."""
     self.assertAllClose(
         gate_with_param.unitary(),
         serializer.deserialize_circuit(
             serializer.serialize_circuit(gate_with_param)).unitary())