Ejemplo n.º 1
0
def _build_gate_proto(gate_id, arg_names, arg_vals, qubit_ids):
    """Helper function to generate proto for a given circuit spec.

    Understand how it works from this example:

    _build_gate_proto("HP",
                      ['exponent', 'global_shift'],
                      ['alpha', 0.0],
                      ['0_0'])

    would produce the following:

    language {
      gate_set: "tfq_gate_set"
    }
    circuit {
      scheduling_strategy: MOMENT_BY_MOMENT
      moments {
        operations {
          gate {
            id: "HP"
          }
          args {
            key: "global_shift"
            value {
              arg_value {
                float_value: 0.0
              }
            }
          }
          args {
            key: "exponent"
            value {
              symbol: "alpha"
            }
          }
          qubits {
            id: "0_0"
          }
        }
      }
    }
    """

    program_proto = program_pb2.Program()
    program_proto.language.gate_set = 'tfq_gate_set'

    circuit_proto = program_proto.circuit
    circuit_proto.scheduling_strategy = circuit_proto.MOMENT_BY_MOMENT  #'1'.
    circuit_proto.moments.add(operations=[program_pb2.Operation(
        gate = program_pb2.Gate(id=gate_id),
        args = {arg_names[i]: (program_pb2.Arg(symbol=arg_vals[i]) \
        if isinstance(arg_vals[i], str) else \
            program_pb2.Arg(
                arg_value=cirq.google.api.v2.program_pb2.ArgValue(
                    float_value=arg_vals[i]))) for i in range(len(arg_vals))},
        qubits=[program_pb2.Qubit(
            id=q_id) for q_id in qubit_ids])])

    return program_proto
Ejemplo n.º 2
0
def _build_circuit_ops_proto(gate_id, arg_names, arg_vals, qubit_ids):
    program_proto = program_pb2.Program()
    program_proto.language.gate_set = 'tfq_gate_set'

    circuit_proto = program_proto.circuit
    circuit_proto.scheduling_strategy = circuit_proto.MOMENT_BY_MOMENT
    circuit_proto.moments.add(operations=[program_pb2.Operation(
        gate = program_pb2.Gate(id=gate_id),
        args = {arg_names[i]: (program_pb2.Arg(symbol=arg_vals[i]) \
        if isinstance(arg_vals[i], str) else \
            program_pb2.Arg(
                arg_value=cirq.google.api.v2.program_pb2.ArgValue(
                    float_value=np.round(float(arg_vals[i]), 6)))) \
                for i in range(len(arg_vals))},
        qubits=[program_pb2.Qubit(
            id=q_id) for q_id in qubit_ids])])
    return program_proto