Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Usage of StateTomography in IBMQ Runtime environment #1226

Closed
MartinBeseda opened this issue Jul 13, 2023 · 4 comments
Closed

Usage of StateTomography in IBMQ Runtime environment #1226

MartinBeseda opened this issue Jul 13, 2023 · 4 comments
Labels
bug Something isn't working

Comments

@MartinBeseda
Copy link

Informations

  • Qiskit Experiments version: 0.24.1
  • Python version: 3.10.12
  • Operating system: Ubuntu 23.04

What is the current behavior?

When I try to obtain results from StateTomography instance by run() function, I'm obtaining the error

RuntimeError: IBMBackend.run() is not supported in the Qiskit Runtime environment.

Steps to reproduce the problem

Run the StateTomography.run() in a Session.

MWE:

import qiskit
from qiskit_experiments.library import StateTomography
from qiskit_ibm_runtime import QiskitRuntimeService, Session

# GHZ State preparation circuit
nq = 2
qc_ghz = qiskit.QuantumCircuit(nq)
qc_ghz.h(0)
qc_ghz.s(0)
for i in range(1, nq):
    qc_ghz.cx(0, i)

service = QiskitRuntimeService()
backend = service.get_backend("ibmq_qasm_simulator")
with Session(service=service, backend=backend) as session:
    # QST Experiment
    qstexp1 = StateTomography(qc_ghz)
    qstdata1 = qstexp1.run(backend, seed_simulation=100).block_for_results()

    # Print results
    for result in qstdata1.analysis_results():
        print(result)

What is the expected behavior?

The expected behavior would be to be able to run Qiskit experiments also under IBMQ Runtime environment.

Suggested solutions

The problem arises in BaseExperiment class, specifically in its _run_jobs() method:

    def _run_jobs(self, circuits: List[QuantumCircuit], **run_options) -> List[Job]:
        """Run circuits on backend as 1 or more jobs."""
        # Get max circuits for job splitting
        max_circuits_option = getattr(self.experiment_options, "max_circuits", None)
        max_circuits_backend = self._backend_data.max_circuits
        if max_circuits_option and max_circuits_backend:
            max_circuits = min(max_circuits_option, max_circuits_backend)
        elif max_circuits_option:
            max_circuits = max_circuits_option
        else:
            max_circuits = max_circuits_backend

        # Run experiment jobs
        if max_circuits and len(circuits) > max_circuits:
            # Split jobs for backends that have a maximum job size
            job_circuits = [
                circuits[i : i + max_circuits] for i in range(0, len(circuits), max_circuits)
            ]
        else:
            # Run as single job
            job_circuits = [circuits]

        # Run jobs
        jobs = [self.backend.run(circs, **run_options) for circs in job_circuits]

        return jobs

It can be seen, that run() is being directly called from IBMBackend instance, as Estimator or Sampler instances are not being used by the class. My suggestion would be to make it possible for them to be provided to a constructor and subsequently to use them during expectation value estimation.

@MartinBeseda MartinBeseda added the bug Something isn't working label Jul 13, 2023
@coruscating
Copy link
Collaborator

Thanks for reporting this. Better Runtime integration is definitely in our plans, see also #1181. In the meantime, you can run experiments with sessions with workaround code like this:

backend_name = "ibmq_qasm_simulator"

exp_tomo = StateTomography(qc_ghz, backend=service.get_backend(backend_name))

def run_jobs(session, circuits, run_options=None):
    jobs = []
    runtime_inputs={'circuits': circuits, 'skip_transpilation': True, **run_options}
    jobs.append(session.run(program_id="circuit-runner", inputs=runtime_inputs))
    return jobs

with Session(service=service, backend=backend_name) as session:
    jobs = run_jobs(session, exp_tomo._transpiled_circuits(), exp_tomo.run_options)

exp_data = exp_tomo._initialize_experiment_data()
exp_data.add_jobs(jobs)
exp_data = exp_tomo.analysis.run(exp_data).block_for_results()

@jyu00
Copy link
Contributor

jyu00 commented Aug 21, 2023

fyi qiskit-ibm-provider is adding session support to backend.run().

But we are also planning to change backend.run() result (will of course go through the deprecation period), so it'd be good to adapt primitives sooner rather than later 😄

@donsano33
Copy link

Just want to mention how easy the workaround now is, thanks to @jyu00's comment that qiskit-ibm-provider backends can now open sessions (as long as we can use backend.run() ):

from qiskit_ibm_provider import IBMProvider
from qiskit_experiments.library.tomography import ProcessTomography

provider = IBMProvider()
backend = provider.get_backend("ibm_nairobi")
qc = QuantumCircuit(1)
qc.x(0)
with backend.open_session() as session:
    tomography = ProcessTomography(qc)
    tomography_result = tomography.run(backend)
    tomography_result.block_for_results()
    session.close()

github-merge-queue bot pushed a commit that referenced this issue Nov 8, 2023
### Summary

This PR adds a sessions how-to based on the discussions in #1226. It
also updates some outdated documentation, such as the links to the
Qiskit Textbook which were all broken.

---------

Co-authored-by: Will Shanks <[email protected]>
@coruscating
Copy link
Collaborator

Note that qiskit-ibm-runtime also supports backend.run() now: Qiskit/qiskit-ibm-runtime#1138. I'm closing this issue now that #1297 has been merged. Feel free to open another issue if you have another use case that isn't supported.

nkanazawa1989 pushed a commit to nkanazawa1989/qiskit-experiments that referenced this issue Jan 17, 2024
### Summary

This PR adds a sessions how-to based on the discussions in qiskit-community#1226. It
also updates some outdated documentation, such as the links to the
Qiskit Textbook which were all broken.

---------

Co-authored-by: Will Shanks <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants