Exemple #1
0
def benchmark_qft_qsim(from_qubits, to_qubits, results):
    gc.disable()

    col_qsim = "qsim"
    results[col_qsim] = np.nan

    for i in range(from_qubits, to_qubits + 1):

        circ = qft_qsim(i)

        qsim_options = {"c": circ, "i": "", "t": 1, "v": 0}

        # Repeat multiple times for small number of qubits and get best time
        repeat = 4 if i <= 20 else 1

        qsim_time = np.nan
        for r in range(repeat):
            gc.collect()
            start_time = time.time()
            result = qsim.qsim_simulate(qsim_options)
            elapsed_time = (time.time() - start_time) * 1000
            qsim_time = elapsed_time if np.isnan(qsim_time) else min(
                qsim_time, elapsed_time)
        results[col_qsim][i] = qsim_time

    gc.enable()
Exemple #2
0
 def test_qsim_simulate(self):
   qsim_options = {
       'c': '2\n0 cnot 0 1\n1 cnot 1 0\n2 cz 0 1\n',
       'i': '00\n01\n10\n11',
       't': 1,
       'v': 0
   }
   self.assertSequenceEqual(
       qsim.qsim_simulate(qsim_options), [(1 + 0j), 0j, 0j, 0j])
Exemple #3
0
    def compute_amplitudes_sweep(
        self,
        program: circuits.Circuit,
        bitstrings: Sequence[int],
        params: study.Sweepable,
        qubit_order: ops.QubitOrderOrList = ops.QubitOrder.DEFAULT,
    ) -> Sequence[Sequence[complex]]:
        """Computes the desired amplitudes using qsim.

      The initial state is assumed to be the all zeros state.

      Args:
          program: The circuit to simulate.
          bitstrings: The bitstrings whose amplitudes are desired, input as an
            string array where each string is formed from measured qubit values
            according to `qubit_order` from most to least significant qubit,
            i.e. in big-endian ordering.
          param_resolver: Parameters to run with the program.
          qubit_order: Determines the canonical ordering of the qubits. This is
            often used in specifying the initial state, i.e. the ordering of the
            computational basis states.

      Returns:
          List of amplitudes.
      """
        if not isinstance(program, qsimc.QSimCircuit):
            program = qsimc.QSimCircuit(program, device=program.device)

        n_qubits = len(program.all_qubits())
        # qsim numbers qubits in reverse order from cirq
        bitstrings = [
            format(bitstring, 'b').zfill(n_qubits)[::-1]
            for bitstring in bitstrings
        ]

        options = {'i': '\n'.join(bitstrings)}
        options.update(self.qsim_options)

        param_resolvers = study.to_resolvers(params)

        trials_results = []
        for prs in param_resolvers:

            solved_circuit = protocols.resolve_parameters(program, prs)

            options['c'] = solved_circuit.translate_cirq_to_qsim(qubit_order)
            options['s'] = self.get_seed()

            amplitudes = qsim.qsim_simulate(options)
            trials_results.append(amplitudes)

        return trials_results