Beispiel #1
0
    def _snapshot_expectation_value(
        self,
        circuit: Circuit,
        hamiltonian: List[Tuple[complex, qk_Pauli]],
        valid_check: bool = True,
    ) -> complex:
        if valid_check:
            self._check_all_circuits([circuit], nomeasure_warn=False)

        circ_qbs = circuit.qubits
        q_indices = (_default_q_index(q) for q in circ_qbs)
        if not all(q_ind == i
                   for q_ind, i in zip(q_indices, range(len(circ_qbs)))):
            raise ValueError(
                "Circuit must act on default register Qubits, contiguously from 0"
                + f" onwards. Circuit qubits were: {circ_qbs}")
        qc = tk_to_qiskit(circuit)
        qc.snapshot_expectation_value("snap", hamiltonian, qc.qubits)
        qobj = assemble(qc)
        job = self._backend.run(qobj)
        return cast(
            complex,
            job.result().data(qc)["snapshots"]["expectation_value"]["snap"][0]
            ["value"],
        )
Beispiel #2
0
    def process_circuits(
        self,
        circuits: Iterable[Circuit],
        n_shots: Optional[int] = None,
        valid_check: bool = True,
        **kwargs: KwargTypes,
    ) -> List[ResultHandle]:
        circuit_list = list(circuits)

        if valid_check:
            self._check_all_circuits(circuit_list)

        qcs = [tk_to_qiskit(tkc) for tkc in circuit_list]
        seed = cast(Optional[int], kwargs.get("seed"))
        qobj = assemble(qcs,
                        shots=n_shots,
                        memory=self._memory,
                        seed_simulator=seed)
        job = self._backend.run(qobj, noise_model=self._noise_model)
        jobid = job.job_id()
        handle_list = [
            ResultHandle(jobid, i) for i in range(len(circuit_list))
        ]
        for handle in handle_list:
            self._cache[handle] = {"job": job}
        return handle_list
Beispiel #3
0
 def process_circuits(
     self,
     circuits: Iterable[Circuit],
     n_shots: Optional[int] = None,
     valid_check: bool = True,
     **kwargs: KwargTypes,
 ) -> List[ResultHandle]:
     """
     See :py:meth:`pytket.backends.Backend.process_circuits`.
     Supported kwargs: none.
     """
     if n_shots is None or n_shots < 1:
         raise ValueError("Parameter n_shots is required for this backend")
     handle_list = []
     for chunk in itertools.zip_longest(*([iter(circuits)] *
                                          self._max_per_job)):
         filtchunk = list(filter(lambda x: x is not None, chunk))
         if valid_check:
             self._check_all_circuits(filtchunk)
         qcs = [tk_to_qiskit(tkc) for tkc in filtchunk]
         qobj = assemble(qcs, shots=n_shots, memory=self._config.memory)
         if self._MACHINE_DEBUG:
             handle_list += [
                 ResultHandle(
                     _DEBUG_HANDLE_PREFIX + str((c.n_qubits, n_shots)), i)
                 for i, c in enumerate(filtchunk)
             ]
         else:
             job = self._backend.run(qobj)
             jobid = job.job_id()
             handle_list += [
                 ResultHandle(jobid, i) for i in range(len(filtchunk))
             ]
     for handle in handle_list:
         self._cache[handle] = dict()
     return handle_list