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

Rj 0.7.update #33

Merged
merged 15 commits into from
Jun 5, 2024
Prev Previous commit
Next Next commit
finished ibm quantum jobs notebook
  • Loading branch information
rjain37 committed May 21, 2024
commit e9e7ffc6fb3b0df3676cbc226f76bd92565ba9fb
136 changes: 39 additions & 97 deletions qbraid_sdk/ibm_quantum_jobs_with_runtime.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
"from qiskit_ibm_runtime import QiskitRuntimeService\n",
"\n",
"service = QiskitRuntimeService(\n",
" channel=\"ibm_quantum\", instance=\"ibm-q/open/main\", token=\"<MY_IBM_QUANTUM_TOKEN>\"\n",
" channel=\"ibm_quantum\", instance=\"ibm-q/open/main\", token=\"7132d362830998e48b83e645bcd400cc1990d8290668252cb5a8fba29111b9eba218fe2f644d25de5b6f1d2658a420b1af5cf9430e3229428ddf717466f2edac\"\n",
")"
]
},
Expand Down Expand Up @@ -67,7 +67,7 @@
"id": "0f8ba878-89b0-458e-8854-034cb7c7b433",
"metadata": {},
"source": [
"The `QiskitRuntimeService.backend()` method (note that this is singular: backend) takes the name of the backend as the input parameter and returns an IBMBackend instance representing that particular backend. The following code will select `ibmq_qasm_simulator` and save it as a `backend_sim`"
"The `QiskitRuntimeService.backend()` method (note that this is singular: backend) takes the name of the backend as the input parameter and returns an IBMBackend instance representing that particular backend. The following code will select `ibm_kyoto` and save it as a `backend_sim`"
]
},
{
Expand All @@ -79,7 +79,7 @@
},
"outputs": [],
"source": [
"backend_sim = service.backend(\"ibmq_qasm_simulator\")"
"backend_sim = service.backend(\"ibm_kyoto\")"
]
},
{
Expand Down Expand Up @@ -112,7 +112,7 @@
"source": [
"### Create a toy circuit\n",
"\n",
"Now, let's create a random circuit by using `qiskit.circuit.random.random_circuit` with 5 qubits with depth=3 with measurement. "
"Now, let's create a simple bell state using qiskit;"
]
},
{
Expand All @@ -124,10 +124,14 @@
},
"outputs": [],
"source": [
"from qiskit.circuit.random import random_circuit\n",
"from qiskit import QuantumCircuit\n",
"\n",
"circ = random_circuit(5, 3, measure=True)\n",
"circ.draw(output=\"mpl\", style=\"iqp\")"
"circ = QuantumCircuit(2)\n",
"circ.h(0)\n",
"circ.cx(0, 1)\n",
"circ.measure_all()\n",
"\n",
"circ.draw()"
]
},
{
Expand All @@ -149,17 +153,25 @@
},
"outputs": [],
"source": [
"from qiskit_ibm_runtime import Sampler, Options\n",
"from qiskit_ibm_runtime import EstimatorV2 as Estimator\n",
"from qiskit.quantum_info import SparsePauliOp\n",
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
"\n",
"options = Options()\n",
"options.resilience_level = 1\n",
"options.optimization_level = 3\n",
"\n",
"# Create an Estimator object\n",
"sampler = Sampler(backend_sim, options=options)\n",
"estimator = Estimator(backend)\n",
"\n",
"# Create observable\n",
"n_qubits = 2\n",
"observable = SparsePauliOp(\"Z\" * n_qubits)\n",
"\n",
"# Submit the circuit to Estimator\n",
"job = sampler.run(circ, shots=10000)"
"# The circuit and observable need to be transformed to only use supported instructions (instruction set architecture(ISA)).\n",
"pm = generate_preset_pass_manager(optimization_level = 1, backend = backend)\n",
"isa_circuit = pm.run(circ)\n",
"isa_observable = observable.apply_layout(isa_circuit.layout)\n",
"\n",
"# Submit the circuit to Sampler\n",
"job = estimator.run([(isa_circuit, isa_observable)])"
]
},
{
Expand Down Expand Up @@ -227,7 +239,7 @@
"source": [
"Now we will plot the results. \n",
"\n",
"As sampler returns quasi probability of measurement, let's use `plot_distribution` with a binary expression. See [SamplerResult document](https://docs.quantum.ibm.com/api/qiskit/qiskit.primitives.SamplerResult) for more information."
"As estimator returns expected value of measurement, we just print out what it is."
]
},
{
Expand All @@ -239,9 +251,7 @@
},
"outputs": [],
"source": [
"from qiskit.visualization import plot_distribution\n",
"\n",
"plot_distribution(result.quasi_dists[0].binary_probabilities())"
"print(job.result()[0].data.evs)"
]
},
{
Expand All @@ -257,9 +267,9 @@
"id": "de67207f-3dd5-499e-bb72-0ad0100b3228",
"metadata": {},
"source": [
"By following [this lab demo](https://github.com/qBraid/qbraid-lab-demo/blob/045c7a8fbdcae66a7e64533dd9fe0e981dc02cf4/qbraid_sdk/ibm_batch_jobs_grovers.ipynb#L4) provided by qBraid, you can also use qbraid sdk to submit your ibm job and check job status. The following show how to do that.\n",
"You can also use qBraid-SDK to submit your IBM job and check job status. The following show how to do that.\n",
"\n",
"First, check the qbraid version."
"First, check the qBraid version."
]
},
{
Expand Down Expand Up @@ -293,15 +303,12 @@
},
"outputs": [],
"source": [
"from qbraid import device_wrapper, job_wrapper, get_jobs\n",
"from qbraid.providers import QuantumJob\n",
"from qbraid.providers.exceptions import JobStateError\n",
"from qbraid.providers.ibm import QiskitBackend, QiskitJob, QiskitProvider\n",
"from qbraid.runtime.qiskit import QiskitRuntimeProvider\n",
"import os\n",
"\n",
"os.environ[\"QISKIT_IBM_TOKEN\"] = \"<MY_IBM_QUANTUM_TOKEN>\"\n",
"os.environ[\"QISKIT_IBM_TOKEN\"] = \"7132d362830998e48b83e645bcd400cc1990d8290668252cb5a8fba29111b9eba218fe2f644d25de5b6f1d2658a420b1af5cf9430e3229428ddf717466f2edac\"\n",
"ibmq_token = os.getenv(\"QISKIT_IBM_TOKEN\")\n",
"provider = QiskitProvider(qiskit_ibm_token=ibmq_token)"
"provider = QiskitRuntimeProvider(ibmq_token)"
]
},
{
Expand Down Expand Up @@ -329,7 +336,7 @@
"id": "a1fb6069-3a00-4250-ad6f-11851ef9089f",
"metadata": {},
"source": [
"`qbraid.providers.ibm` also supports useful filtering by `get_devices()`. You can quickly find the least busy backend by using `ibm_least_busy_gpu()`."
"You can quickly find the least busy device by using `ibm_least_busy_gpu()`, or use `get_devices()` to see all the available devices."
]
},
{
Expand All @@ -342,7 +349,7 @@
"outputs": [],
"source": [
"# ibm_device = provider.ibm_least_busy_qpu() # return least busy backend of provider\n",
"# ibm_device = provider.get_devices(operational=True, simulator=False) # return list of operational QPU backends\n",
"# ibm_device = provider.get_devices() # return list of all backends\n",
"ibm_device = provider.get_device(\"ibm_kyoto\") # return backend by name"
]
},
Expand All @@ -351,28 +358,7 @@
"id": "545c1b43-9679-4dd6-ad6c-3dc7a48833df",
"metadata": {},
"source": [
"To send the quantum circuit to the backend and check the job status in the right sidebar of qBraid Quantum lab, wrap the ibm backend with `device_wrapper`. If you insert the backend, which does not appear in the right sidebar panel's \"device\" section, this code will return an error."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "086710ef-5cac-4fe3-95f0-d071c8321cea",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"# qbraid_ibm_device = QiskitBackend(ibm_device) # you can load device object directly as well\n",
"qbraid_ibm_device = device_wrapper(\"ibm_kyoto\")"
]
},
{
"cell_type": "markdown",
"id": "953be90a-d983-444d-8d1f-4ebe9cd9f0e8",
"metadata": {},
"source": [
"To send a quantum circuit, you can simply call `wrapped_device.run(circuit, options)`. See [API document](https://docs.qbraid.com/en/stable/sdk/devices.html#device-wrapper) for more information."
"To send the quantum circuit to the backend and check the job status in the right sidebar of qBraid Quantum lab, we just run the device."
]
},
{
Expand All @@ -385,17 +371,15 @@
"outputs": [],
"source": [
"# Must have IBM credential to submit jobs using premium backends\n",
"qbraid_ibm_job = qbraid_ibm_device.run(circ, shots=20000)"
"qbraid_ibm_job = ibm_device.run(circ, shots=200)"
]
},
{
"cell_type": "markdown",
"id": "52c2510a-2a69-4624-aede-56237b76fe53",
"metadata": {},
"source": [
"Shortly afterwards you should see your submitted job in the right side panel. If you cannot see your job, click the circulation icon at the top to refresh or select `All` for Provider.\n",
"\n",
"Also, you can check your job status by using `job_wrapper(job_id).` Please note that the `job_id` for `job_wrapper` must be a qBraidID, which you can get by adding `.id` to your job."
"Also, you can check your job status by using `status()`."
]
},
{
Expand All @@ -407,48 +391,6 @@
},
"outputs": [],
"source": [
"job = job_wrapper(qbraid_ibm_job.id)\n",
"job.status()"
]
},
{
"cell_type": "markdown",
"id": "2c65961f-540e-43dc-b318-c57d86e31751",
"metadata": {},
"source": [
"You can use the `get_jobs` function to a return a list of your previously submitted quantum jobs, along with the status of each."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "3523429e-3341-4a57-80e3-2ad2fb9d3871",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"get_jobs()"
]
},
{
"cell_type": "markdown",
"id": "4dedeb3d-a03c-4b70-b9f9-a7a7000eb045",
"metadata": {},
"source": [
"This `qBraidID` can be used to reinstantiate a qBraid QuantumJob object at any time, and even in a separate program, with no loss of information."
]
},
{
"cell_type": "code",
"execution_count": null,
"id": "848add3e-1059-4726-be73-855fb8a7adad",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"job = job_wrapper(\"<MY_qBraid_Job_ID>\")\n",
"job.status()"
]
},
Expand Down Expand Up @@ -521,7 +463,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.13"
"version": "3.11.9"
}
},
"nbformat": 4,
Expand Down