656 lines
32 KiB
Plaintext
656 lines
32 KiB
Plaintext
{
|
||
"cells": [
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "915d2c66-aed3-47f1-a405-144348514dd3",
|
||
"metadata": {},
|
||
"source": [
|
||
"# Primitives examples\n",
|
||
"\n",
|
||
"The examples in this section illustrate some common ways to use primitives. Before running these examples, follow the instructions in [Install and set up.](install-qiskit)\n",
|
||
"\n",
|
||
"<Admonition type=\"note\">\n",
|
||
" These examples all use the primitives from Qiskit Runtime, but you could use the base primitives instead.\n",
|
||
"</Admonition>\n",
|
||
"\n",
|
||
"## Estimator examples\n",
|
||
"\n",
|
||
"Efficiently calculate and interpret expectation values of the quantum operators required for many algorithms with Estimator. Explore uses in molecular modeling, machine learning, and complex optimization problems.\n",
|
||
"\n",
|
||
"### Run a single experiment\n",
|
||
"\n",
|
||
"Use Estimator to determine the expectation value of a single circuit-observable pair."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 1,
|
||
"id": "88d9d34b-2903-48b9-8f3c-398da05b487b",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
" > Expectation value: -0.7916666666666666\n",
|
||
" > Metadata: {'shots': 4096, 'target_precision': 0.015625, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 32}\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import numpy as np\n",
|
||
"from qiskit.circuit.library import IQP\n",
|
||
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
|
||
"from qiskit.quantum_info import SparsePauliOp, random_hermitian\n",
|
||
"from qiskit_ibm_runtime import QiskitRuntimeService, EstimatorV2 as Estimator\n",
|
||
"\n",
|
||
"n_qubits = 50\n",
|
||
"\n",
|
||
"service = QiskitRuntimeService()\n",
|
||
"backend = service.least_busy(\n",
|
||
" operational=True, simulator=False, min_num_qubits=n_qubits\n",
|
||
")\n",
|
||
"\n",
|
||
"mat = np.real(random_hermitian(n_qubits, seed=1234))\n",
|
||
"circuit = IQP(mat)\n",
|
||
"observable = SparsePauliOp(\"Z\" * 50)\n",
|
||
"\n",
|
||
"pm = generate_preset_pass_manager(backend=backend, optimization_level=1)\n",
|
||
"isa_circuit = pm.run(circuit)\n",
|
||
"isa_observable = observable.apply_layout(isa_circuit.layout)\n",
|
||
"\n",
|
||
"estimator = Estimator(mode=backend)\n",
|
||
"job = estimator.run([(isa_circuit, isa_observable)])\n",
|
||
"result = job.result()\n",
|
||
"\n",
|
||
"print(f\" > Expectation value: {result[0].data.evs}\")\n",
|
||
"print(f\" > Metadata: {result[0].metadata}\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "191eb3ee-b0d7-4710-90d6-1839ab51719e",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Run multiple experiments in a single job\n",
|
||
"\n",
|
||
"Use Estimator to determine the expectation values of multiple circuit-observable pairs."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 2,
|
||
"id": "6bd60dea-dde5-48a6-9b35-7d90a48692a1",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
">>> Expectation values for PUB 0: -0.08808290155440414\n",
|
||
">>> Standard errors for PUB 0: 0.07584969096715767\n",
|
||
">>> Expectation values for PUB 1: -0.07734806629834254\n",
|
||
">>> Standard errors for PUB 1: 0.0941405420654118\n",
|
||
">>> Expectation values for PUB 2: -0.03985507246376811\n",
|
||
">>> Standard errors for PUB 2: 0.11134528787244734\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import numpy as np\n",
|
||
"from qiskit.circuit.library import IQP\n",
|
||
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
|
||
"from qiskit.quantum_info import SparsePauliOp, random_hermitian\n",
|
||
"from qiskit_ibm_runtime import QiskitRuntimeService, EstimatorV2 as Estimator\n",
|
||
"\n",
|
||
"n_qubits = 50\n",
|
||
"\n",
|
||
"service = QiskitRuntimeService()\n",
|
||
"backend = service.least_busy(\n",
|
||
" operational=True, simulator=False, min_num_qubits=n_qubits\n",
|
||
")\n",
|
||
"\n",
|
||
"rng = np.random.default_rng()\n",
|
||
"mats = [np.real(random_hermitian(n_qubits, seed=rng)) for _ in range(3)]\n",
|
||
"\n",
|
||
"pubs = []\n",
|
||
"circuits = [IQP(mat) for mat in mats]\n",
|
||
"observables = [\n",
|
||
" SparsePauliOp(\"X\" * 50),\n",
|
||
" SparsePauliOp(\"Y\" * 50),\n",
|
||
" SparsePauliOp(\"Z\" * 50),\n",
|
||
"]\n",
|
||
"\n",
|
||
"# Get ISA circuits\n",
|
||
"pm = generate_preset_pass_manager(optimization_level=1, backend=backend)\n",
|
||
"\n",
|
||
"for qc, obs in zip(circuits, observables):\n",
|
||
" isa_circuit = pm.run(qc)\n",
|
||
" isa_obs = obs.apply_layout(isa_circuit.layout)\n",
|
||
" pubs.append((isa_circuit, isa_obs))\n",
|
||
"\n",
|
||
"estimator = Estimator(backend)\n",
|
||
"job = estimator.run(pubs)\n",
|
||
"job_result = job.result()\n",
|
||
"\n",
|
||
"for idx in range(len(pubs)):\n",
|
||
" pub_result = job_result[idx]\n",
|
||
" print(f\">>> Expectation values for PUB {idx}: {pub_result.data.evs}\")\n",
|
||
" print(f\">>> Standard errors for PUB {idx}: {pub_result.data.stds}\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "36995a0d-a912-4a55-8ed5-20525d248237",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Run parameterized circuits\n",
|
||
"\n",
|
||
"Use Estimator to run three experiments in a single job, leveraging parameter values to increase circuit reusability."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 3,
|
||
"id": "c80000d6-b8fe-419c-9d4c-874190b9b8b7",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
">>> Expectation values: [[ 9.92596584e-01 9.35114259e-01 7.94487856e-01 5.66406200e-01\n",
|
||
" 2.66882225e-01 -3.32576312e-02 -3.35450428e-01 -5.85703838e-01\n",
|
||
" -8.13990787e-01 -9.52974839e-01 -9.93212466e-01 -9.37783081e-01\n",
|
||
" -7.93461385e-01 -5.70717374e-01 -2.77146926e-01 2.21717541e-02\n",
|
||
" 3.23543375e-01 5.99458537e-01 8.15222552e-01 9.44968372e-01\n",
|
||
" 9.91364820e-01]\n",
|
||
" [ 1.88870498e-02 3.22722199e-01 6.06849122e-01 8.18917844e-01\n",
|
||
" 9.54822485e-01 9.89106586e-01 9.40862491e-01 7.89355505e-01\n",
|
||
" 5.68048552e-01 2.77146926e-01 -2.09399900e-02 -3.19232200e-01\n",
|
||
" -5.93915598e-01 -8.13169611e-01 -9.51127192e-01 -9.94033643e-01\n",
|
||
" -9.34293083e-01 -7.80527862e-01 -5.62710908e-01 -2.91517508e-01\n",
|
||
" 2.29929302e-02]\n",
|
||
" [-8.21176078e-04 -3.11020440e-01 -5.87962072e-01 -8.12348435e-01\n",
|
||
" -9.42299550e-01 -9.96702465e-01 -9.45378960e-01 -8.02083734e-01\n",
|
||
" -5.75644431e-01 -2.94186330e-01 -8.21176078e-04 3.11431028e-01\n",
|
||
" 5.82008545e-01 8.06600203e-01 9.54206603e-01 9.91364820e-01\n",
|
||
" 9.46816018e-01 8.01057264e-01 5.79339723e-01 3.03424561e-01\n",
|
||
" 1.84764618e-03]\n",
|
||
" [ 9.94649525e-01 9.42299550e-01 7.97567266e-01 5.88167366e-01\n",
|
||
" 2.95828682e-01 -1.78605797e-02 -3.14510438e-01 -6.00074419e-01\n",
|
||
" -8.13169611e-01 -9.52769545e-01 -9.93007172e-01 -9.49690134e-01\n",
|
||
" -8.03315498e-01 -5.80571487e-01 -2.98702798e-01 1.95029319e-02\n",
|
||
" 3.18000436e-01 5.97610891e-01 8.03726086e-01 9.48868958e-01\n",
|
||
" 9.95265407e-01]]\n",
|
||
">>> Standard errors: [[0.00600824 0.00658169 0.00790535 0.00856936 0.00944475 0.00984685\n",
|
||
" 0.00859838 0.00801389 0.00734307 0.00600556 0.00575004 0.00610699\n",
|
||
" 0.00803626 0.00804232 0.00905999 0.01037867 0.01144714 0.00919335\n",
|
||
" 0.00804772 0.00655358 0.0058244 ]\n",
|
||
" [0.01052767 0.00876847 0.00957375 0.00754635 0.00628896 0.00590061\n",
|
||
" 0.00641098 0.00772734 0.00987145 0.00974144 0.00820962 0.01125198\n",
|
||
" 0.00917579 0.00686203 0.00693163 0.00581229 0.00624401 0.00664373\n",
|
||
" 0.00898001 0.01015663 0.01187577]\n",
|
||
" [0.00950026 0.0101949 0.00861343 0.00719445 0.00636213 0.00589903\n",
|
||
" 0.00670304 0.00664176 0.00922313 0.01069824 0.01062285 0.00818293\n",
|
||
" 0.00869165 0.00780179 0.00628562 0.00598147 0.0060332 0.00748056\n",
|
||
" 0.00941353 0.01112703 0.01032503]\n",
|
||
" [0.00568269 0.00629553 0.00785744 0.00800247 0.00970712 0.01307399\n",
|
||
" 0.00829052 0.00987137 0.00826613 0.00603997 0.00605031 0.00638605\n",
|
||
" 0.00739553 0.00927726 0.01084354 0.01159906 0.01069012 0.00806646\n",
|
||
" 0.00692216 0.0062018 0.00560085]]\n",
|
||
">>> Metadata: {'shots': 10016, 'target_precision': 0.01, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 32}\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import numpy as np\n",
|
||
"\n",
|
||
"from qiskit.circuit import QuantumCircuit, Parameter\n",
|
||
"from qiskit.quantum_info import SparsePauliOp\n",
|
||
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
|
||
"from qiskit_ibm_runtime import QiskitRuntimeService, EstimatorV2 as Estimator\n",
|
||
"\n",
|
||
"service = QiskitRuntimeService()\n",
|
||
"backend = service.least_busy(operational=True, simulator=False)\n",
|
||
"\n",
|
||
"# Step 1: Map classical inputs to a quantum problem\n",
|
||
"theta = Parameter(\"θ\")\n",
|
||
"\n",
|
||
"chsh_circuit = QuantumCircuit(2)\n",
|
||
"chsh_circuit.h(0)\n",
|
||
"chsh_circuit.cx(0, 1)\n",
|
||
"chsh_circuit.ry(theta, 0)\n",
|
||
"\n",
|
||
"number_of_phases = 21\n",
|
||
"phases = np.linspace(0, 2 * np.pi, number_of_phases)\n",
|
||
"individual_phases = [[ph] for ph in phases]\n",
|
||
"\n",
|
||
"ZZ = SparsePauliOp.from_list([(\"ZZ\", 1)])\n",
|
||
"ZX = SparsePauliOp.from_list([(\"ZX\", 1)])\n",
|
||
"XZ = SparsePauliOp.from_list([(\"XZ\", 1)])\n",
|
||
"XX = SparsePauliOp.from_list([(\"XX\", 1)])\n",
|
||
"ops = [ZZ, ZX, XZ, XX]\n",
|
||
"\n",
|
||
"# Step 2: Optimize problem for quantum execution.\n",
|
||
"\n",
|
||
"pm = generate_preset_pass_manager(backend=backend, optimization_level=1)\n",
|
||
"chsh_isa_circuit = pm.run(chsh_circuit)\n",
|
||
"isa_observables = [\n",
|
||
" operator.apply_layout(chsh_isa_circuit.layout) for operator in ops\n",
|
||
"]\n",
|
||
"\n",
|
||
"# Step 3: Execute using Qiskit primitives.\n",
|
||
"\n",
|
||
"# Reshape observable array for broadcasting\n",
|
||
"reshaped_ops = np.fromiter(isa_observables, dtype=object)\n",
|
||
"reshaped_ops = reshaped_ops.reshape((4, 1))\n",
|
||
"\n",
|
||
"estimator = Estimator(backend, options={\"default_shots\": int(1e4)})\n",
|
||
"job = estimator.run([(chsh_isa_circuit, reshaped_ops, individual_phases)])\n",
|
||
"# Get results for the first (and only) PUB\n",
|
||
"pub_result = job.result()[0]\n",
|
||
"print(f\">>> Expectation values: {pub_result.data.evs}\")\n",
|
||
"print(f\">>> Standard errors: {pub_result.data.stds}\")\n",
|
||
"print(f\">>> Metadata: {pub_result.metadata}\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "c7978aa5-da67-4f93-927d-277802ae5437",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Use sessions and advanced options\n",
|
||
"\n",
|
||
"Explore sessions and advanced options to optimize circuit performance on QPUs."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 4,
|
||
"id": "cbe2bed7-e5c1-4264-ac42-1c267ff1af3b",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
" > Expectation value: 0.6953125\n",
|
||
" > Metadata: {'shots': 4096, 'target_precision': 0.015625, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 32}\n",
|
||
" > Another Expectation value: 0.0211864406779661\n",
|
||
" > More Metadata: {'shots': 4096, 'target_precision': 0.015625, 'circuit_metadata': {}, 'resilience': {}, 'num_randomizations': 32}\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import numpy as np\n",
|
||
"from qiskit.circuit.library import IQP\n",
|
||
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
|
||
"from qiskit.quantum_info import SparsePauliOp, random_hermitian\n",
|
||
"from qiskit_ibm_runtime import (\n",
|
||
" QiskitRuntimeService,\n",
|
||
" Session,\n",
|
||
" EstimatorV2 as Estimator,\n",
|
||
")\n",
|
||
"\n",
|
||
"n_qubits = 50\n",
|
||
"\n",
|
||
"service = QiskitRuntimeService()\n",
|
||
"backend = service.least_busy(\n",
|
||
" operational=True, simulator=False, min_num_qubits=n_qubits\n",
|
||
")\n",
|
||
"\n",
|
||
"rng = np.random.default_rng(1234)\n",
|
||
"mat = np.real(random_hermitian(n_qubits, seed=rng))\n",
|
||
"circuit = IQP(mat)\n",
|
||
"mat = np.real(random_hermitian(n_qubits, seed=rng))\n",
|
||
"another_circuit = IQP(mat)\n",
|
||
"observable = SparsePauliOp(\"X\" * 50)\n",
|
||
"another_observable = SparsePauliOp(\"Y\" * 50)\n",
|
||
"\n",
|
||
"pm = generate_preset_pass_manager(optimization_level=1, backend=backend)\n",
|
||
"isa_circuit = pm.run(circuit)\n",
|
||
"another_isa_circuit = pm.run(another_circuit)\n",
|
||
"isa_observable = observable.apply_layout(isa_circuit.layout)\n",
|
||
"another_isa_observable = another_observable.apply_layout(\n",
|
||
" another_isa_circuit.layout\n",
|
||
")\n",
|
||
"\n",
|
||
"with Session(backend=backend) as session:\n",
|
||
" estimator = Estimator(mode=session)\n",
|
||
"\n",
|
||
" estimator.options.resilience_level = 1\n",
|
||
"\n",
|
||
" job = estimator.run([(isa_circuit, isa_observable)])\n",
|
||
" another_job = estimator.run(\n",
|
||
" [(another_isa_circuit, another_isa_observable)]\n",
|
||
" )\n",
|
||
" result = job.result()\n",
|
||
" another_result = another_job.result()\n",
|
||
"\n",
|
||
" # first job\n",
|
||
" print(f\" > Expectation value: {result[0].data.evs}\")\n",
|
||
" print(f\" > Metadata: {result[0].metadata}\")\n",
|
||
"\n",
|
||
" # second job\n",
|
||
" print(f\" > Another Expectation value: {another_result[0].data.evs}\")\n",
|
||
" print(f\" > More Metadata: {another_result[0].metadata}\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "3a60b53b-418a-44fe-92d2-b1ba5d967c84",
|
||
"metadata": {},
|
||
"source": [
|
||
"<span id=\"sampler-examples\"></span>\n",
|
||
"## Sampler examples\n",
|
||
"\n",
|
||
"Generate entire error-mitigated quasi-probability distributions sampled from quantum circuit outputs. Leverage Sampler’s capabilities for search and classification algorithms like Grover’s and QVSM.\n",
|
||
"\n",
|
||
"### Run a single experiment\n",
|
||
"\n",
|
||
"Use Sampler to return the measurement outcome as bitstrings or counts of a single circuit."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 5,
|
||
"id": "012cfeea-3af3-46bb-9e64-15bb052bbcd2",
|
||
"metadata": {
|
||
"scrolled": true
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
" > First ten results: ['1001011110001101110100010101110001010010111110000111001110110000010100000100000100000000001100101010111010001011011010010000001', '0010011010010011111100100110111100001110001001101001010011000100010001001010010010100000100000100011000111110110011000001001011', '0000100111100110001100111100000111010100100101001111001110101101110011011011100110000000100100001100111010001000010001000001000', '1000011010001000111000000100110101001001001100111110110011000010100011101000110001000011001101000011001111000010000000100000011', '0100010111000101010000101000010100110010101001011001000010001000110000010000000011000010001100001101100101011011000000101110001', '1111111110011100110100110000000000101100000010000111010011000000000001110001100001100100000100101010011010000110000011100000011', '1111111010001011100000100010000010011000100010100000100100100110101011101011000001101101100100000001110011101010001010100010000', '0010001101011110101010010100001000001001111011000000000100101010101011001000000100000000101100011110111010010100001110101000000', '1000001000001000000000111000011101010100001100100010011011000110110111010001100001000000111001011010110001001010010000100000010', '1010001100000100011010111000100100000000001011100100100011010010010100100001110001001100000001001100110100001110011000000011001']\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import numpy as np\n",
|
||
"from qiskit.circuit.library import IQP\n",
|
||
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
|
||
"from qiskit.quantum_info import random_hermitian\n",
|
||
"from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2 as Sampler\n",
|
||
"\n",
|
||
"n_qubits = 127\n",
|
||
"\n",
|
||
"service = QiskitRuntimeService()\n",
|
||
"backend = service.least_busy(\n",
|
||
" operational=True, simulator=False, min_num_qubits=n_qubits\n",
|
||
")\n",
|
||
"\n",
|
||
"mat = np.real(random_hermitian(n_qubits, seed=1234))\n",
|
||
"circuit = IQP(mat)\n",
|
||
"circuit.measure_all()\n",
|
||
"\n",
|
||
"pm = generate_preset_pass_manager(backend=backend, optimization_level=1)\n",
|
||
"isa_circuit = pm.run(circuit)\n",
|
||
"\n",
|
||
"sampler = Sampler(backend)\n",
|
||
"job = sampler.run([isa_circuit])\n",
|
||
"result = job.result()\n",
|
||
"\n",
|
||
"# Get results for the first (and only) PUB\n",
|
||
"pub_result = result[0]\n",
|
||
"\n",
|
||
"print(f\" > First ten results: {pub_result.data.meas.get_bitstrings()[:10]}\")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "a5c3d65c-9d14-4b4e-800c-6e3e231e335c",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Run multiple experiments in a single job\n",
|
||
"\n",
|
||
"Use Sampler to return the measurement outcome as bitstrings or counts of multiple circuits in one job."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 6,
|
||
"id": "b5372b7e-8bc2-4f2e-8f65-03dcd368faeb",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
" > First ten results for pub 0: ['0001111010010000100010010011111111000011000101000101101101000111010010101010101010110100010100011100101101101111100010110101100', '0100010010011010111010000010011001000000110011010111000000000001011010100100101001000010111101011011000100001001010111011000011', '1101011010010001000011011111000010010110001100110000111011010110100100001000011000101110001100001000100011101111001010100011010', '1010110010100100111111101001111011010000110000111000000000001111000100111100000011100000001101011000010011011101010111001110100', '1111011110100011100010001100010000010000011110100000010111000110000100101001001100100100000001001010000001010110000100100110011', '0110000001000011000110000000110001001010100000100111001110011100010100001011010010000000110000001100010101010110001011000010111', '1111011000110001110101000011010010000100100111010000011110111111011000011010001011100111110100010000001010110110010000010111010', '1010011101100101000001011111001001010000100011111110001110100111000000100011000001111111001101011110000010000011000100000011011', '0111011010110001011100010011011100010010001011110000001100100111110010010011101011001101100101000110011011010100000010110001100', '1101000000011011111011010011110010010101011010111010110001111001001000110010100001101001011110010101111101110000000011100100100']\n",
|
||
" > First ten results for pub 1: ['0011111110010111100101000010010100100000101000000000100101001100110101101110000000010000100101100101000001011011100010000011100', '0111001110000000001010000110101100011101110010010100010111001001100111110010000000010011001110000101010010001110100000000110101', '0010110110010011011101000011001100000100100111010100000000001000101000100011010011010010001110011010000110001111100000000110100', '1011000101100011011001010100001100011100010111001000100010010100011011011110100100011101001111101000100100010011001100000110000', '0100000100111000001011011011111010101100111111100101110100110000100000111101111000000000110011101101001100011001111000110110110', '0100011010110111101011110010111001111111110111101010110010010101000011100111100111000001110101010110000110101010100000100100001', '0101011011000110001011111111101010001001010010001000111100000110100010100111101010010001000101110100010110011010000110000000100', '1000001010100001100001001010000110001011110110011111000101000111110000001101000000001000001101100100010011011000011100000110010', '1111110000111111100100111101110010100110110001110000100010000100011101001110000010000001101100110001001101110100100100000000001', '1000100110101101010001001100010101011001010010111000010100101100011111001000000001000000100000000101101001100001101101000110100']\n",
|
||
" > First ten results for pub 2: ['0100010000100100101110100101111000001000011101001010001001001001011101101101010000001111100000001100110010000000000100000111001', '0101110110101101000101101100010001110001110000000111100100101000010110100101000000100010001000100111101110100100000001000001000', '0011110001011010100110100111000010010011100100110001000010100101001011000010000111100100010001110000010110100110111000000010110', '1110111101010011000110001100001010100010010010001111101001001000010010011001001010011010000011111011100111000110000001101100110', '0001010100001010011010010101111000011111010000101110001001000011100101010001001011001101100000110001000100000000001001011011010', '1100001100010111110010000110101010000010101101011100011000001100001011100011001101011001100100111000010010000110001000001000100', '0011110011000011111100100001101001110010101101110011010000101111010000000000011110001010101001110001111100011000101000000000100', '1100000100010101011010011110000110110111101010001001111110001110011010001111010000100010010000100100101010101110100000001100010', '1110110111000010111100011101010010101100001011101010110100000101010101100000101011100001011000110010101010100100110001000100000', '1011110100101000110011010010100011010000000001000000000000010010000101110100001001110100000011100010111100000100001110010100110']\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import numpy as np\n",
|
||
"from qiskit.circuit.library import IQP\n",
|
||
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
|
||
"from qiskit.quantum_info import random_hermitian\n",
|
||
"from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2 as Sampler\n",
|
||
"\n",
|
||
"n_qubits = 127\n",
|
||
"\n",
|
||
"service = QiskitRuntimeService()\n",
|
||
"backend = service.least_busy(\n",
|
||
" operational=True, simulator=False, min_num_qubits=n_qubits\n",
|
||
")\n",
|
||
"\n",
|
||
"rng = np.random.default_rng()\n",
|
||
"mats = [np.real(random_hermitian(n_qubits, seed=rng)) for _ in range(3)]\n",
|
||
"circuits = [IQP(mat) for mat in mats]\n",
|
||
"for circuit in circuits:\n",
|
||
" circuit.measure_all()\n",
|
||
"\n",
|
||
"pm = generate_preset_pass_manager(backend=backend, optimization_level=1)\n",
|
||
"isa_circuits = pm.run(circuits)\n",
|
||
"\n",
|
||
"sampler = Sampler(mode=backend)\n",
|
||
"job = sampler.run(isa_circuits)\n",
|
||
"result = job.result()\n",
|
||
"\n",
|
||
"for idx, pub_result in enumerate(result):\n",
|
||
" print(\n",
|
||
" f\" > First ten results for pub {idx}: {pub_result.data.meas.get_bitstrings()[:10]}\"\n",
|
||
" )"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "36337791-94ff-4ed5-aa26-808c470eb728",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Run parameterized circuits\n",
|
||
"\n",
|
||
"Run several experiments in a single job, leveraging parameter values to increase circuit reusability."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 7,
|
||
"id": "ea6c113e-15db-47a1-be34-459e68d1db4e",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
" >> First ten results for the meas output register: ['0000100100011000101000001100101101011100000011110011011011001010000110101101100010001000010010110111001011111010101110101100001', '0100111100011100000110010100010011000110110111110011111011001001100110110001011101001101000011100001010011011000101101010010110', '1111010110101101101111010100110101010000111001111101001010111100111110101001110011111010010010101001010110110011110100011110001', '0101010100111001101010010100110001010101001111010011101000011001000110101110101001111110011001110111101011100001101110110000101', '1110001000100101001011010101011001010100100000001111101111101101000010001110111000101111110010000001010011000011001001010110101', '1111111001110101010010000100001101000001111001110011100000110011001011111100111011100011101000010101010000100111111110111110011', '0100111000110111111110011101011101110000100001001001001000011101011101001111111101111100001000001000001111011001111100010000101', '1001110110001111001010000110000110000101110111110000000111101111001100010111100101100001001011011111100101101100000011010110101', '0101000001001000100011010100100001111010101100011001011010111001000111001111011000010000010001011100010101110011111101111100001', '0001100101110111101001000110110000110101011111001110101000000000110000001101100010111010110011001110111111001110011111010000111']\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import numpy as np\n",
|
||
"from qiskit.circuit.library import RealAmplitudes\n",
|
||
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
|
||
"from qiskit_ibm_runtime import QiskitRuntimeService, SamplerV2 as Sampler\n",
|
||
"\n",
|
||
"n_qubits = 127\n",
|
||
"\n",
|
||
"service = QiskitRuntimeService()\n",
|
||
"backend = service.least_busy(\n",
|
||
" operational=True, simulator=False, min_num_qubits=n_qubits\n",
|
||
")\n",
|
||
"\n",
|
||
"# Step 1: Map classical inputs to a quantum problem\n",
|
||
"circuit = RealAmplitudes(num_qubits=n_qubits, reps=2)\n",
|
||
"circuit.measure_all()\n",
|
||
"\n",
|
||
"# Define three sets of parameters for the circuit\n",
|
||
"rng = np.random.default_rng(1234)\n",
|
||
"parameter_values = [\n",
|
||
" rng.uniform(-np.pi, np.pi, size=circuit.num_parameters) for _ in range(3)\n",
|
||
"]\n",
|
||
"\n",
|
||
"# Step 2: Optimize problem for quantum execution.\n",
|
||
"\n",
|
||
"pm = generate_preset_pass_manager(backend=backend, optimization_level=1)\n",
|
||
"isa_circuit = pm.run(circuit)\n",
|
||
"\n",
|
||
"# Step 3: Execute using Qiskit primitives.\n",
|
||
"sampler = Sampler(backend)\n",
|
||
"job = sampler.run([(isa_circuit, parameter_values)])\n",
|
||
"result = job.result()\n",
|
||
"# Get results for the first (and only) PUB\n",
|
||
"pub_result = result[0]\n",
|
||
"# Get counts from the classical register \"meas\".\n",
|
||
"print(\n",
|
||
" f\" >> First ten results for the meas output register: {pub_result.data.meas.get_bitstrings()[:10]}\"\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "1f144ac9-5f64-4aca-8cc4-8cf9362c5666",
|
||
"metadata": {},
|
||
"source": [
|
||
"### Use sessions and advanced options\n",
|
||
"\n",
|
||
"Explore sessions and advanced options to optimize circuit performance on QPUs."
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 8,
|
||
"id": "d6248dcb-2ba9-4a06-a9ef-727db7092093",
|
||
"metadata": {
|
||
"scrolled": true
|
||
},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
" > The first ten measurement results of job 1: ['0000101101011011001000000100111000100001001100000010000110001011111101010111000100100101110011100110001000001011010100100000011', '1000001010101111111000000110000011111000000101010000000100100111001001001110010010110010001011101010000001100010100111101111101', '1011010111110000000111110111001100001100101000000011010000010101101110000011011001010010011000101001111110100100011100100000101', '0000111011001011110001000101010000101001101000000010010101111010100110101000101111001110000000101000000101110100110010001100101', '1001011001101000000100111010101011100001111101000000000111101101110100111100110101100001000100001101100010110010100110101000010', '0100010010001011000011110000101001101110101001000101110100000110101111000000110110101000010100000000101100010010110101100100010', '0010100111110110010101011110010111000000000101101000000011011011110111000000011100011100110000101000101011111100000010010100111', '0001010010100110010111000100010100100100111001001000000010000111000010101011101010111010000110000000101100000011011011000110000', '0010010111001001100011101100001111111010010001100011010100000000110001011010000010001111001010001000011010010010000101000001101', '1001011100100000010011000011010001000100110000011111000111101110100001100001110101111111000101000000101001101001001011100000001']\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"import numpy as np\n",
|
||
"from qiskit.circuit.library import IQP\n",
|
||
"from qiskit.quantum_info import random_hermitian\n",
|
||
"from qiskit.transpiler.preset_passmanagers import generate_preset_pass_manager\n",
|
||
"from qiskit_ibm_runtime import Session, SamplerV2 as Sampler\n",
|
||
"from qiskit_ibm_runtime import QiskitRuntimeService\n",
|
||
"\n",
|
||
"n_qubits = 127\n",
|
||
"\n",
|
||
"service = QiskitRuntimeService()\n",
|
||
"backend = service.least_busy(\n",
|
||
" operational=True, simulator=False, min_num_qubits=n_qubits\n",
|
||
")\n",
|
||
"\n",
|
||
"rng = np.random.default_rng(1234)\n",
|
||
"mat = np.real(random_hermitian(n_qubits, seed=rng))\n",
|
||
"circuit = IQP(mat)\n",
|
||
"circuit.measure_all()\n",
|
||
"mat = np.real(random_hermitian(n_qubits, seed=rng))\n",
|
||
"another_circuit = IQP(mat)\n",
|
||
"another_circuit.measure_all()\n",
|
||
"\n",
|
||
"pm = generate_preset_pass_manager(backend=backend, optimization_level=1)\n",
|
||
"isa_circuit = pm.run(circuit)\n",
|
||
"another_isa_circuit = pm.run(another_circuit)\n",
|
||
"\n",
|
||
"with Session(backend=backend) as session:\n",
|
||
" sampler = Sampler(mode=session)\n",
|
||
" job = sampler.run([isa_circuit])\n",
|
||
" another_job = sampler.run([another_isa_circuit])\n",
|
||
" result = job.result()\n",
|
||
" another_result = another_job.result()\n",
|
||
"\n",
|
||
"# first job\n",
|
||
"\n",
|
||
"print(\n",
|
||
" f\" > The first ten measurement results of job 1: {result[0].data.meas.get_bitstrings()[:10]}\"\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "code",
|
||
"execution_count": 9,
|
||
"id": "e81eb3e4-632a-49da-a926-6e79f8640867",
|
||
"metadata": {},
|
||
"outputs": [
|
||
{
|
||
"name": "stdout",
|
||
"output_type": "stream",
|
||
"text": [
|
||
" > The first ten measurement results of job 2: ['1011000011100011001100010011100010001011000010110100110011011001001010110100000001001100010110100000001010010000000101000100011', '1011101111000010110001101010111010110101100000101100010101011101001110000000001001000001001101001100010011000010001100001111010', '1001101111100010011100101010001011011000010110110110111110010110011001010000010111000000100001000111110111000010000001100000001', '0011110000101001000000111001100101000111000110100010100011001111101100000010010010000000001011101110111001001110011000000101010', '1011100100001000010110100001110101101010001011011010001101001010100001110100010100100001111001010110111101000000100000111110010', '1100100011100111111110110101100000111011101000000100111001010111101110110000101011101111001001001010001101011010100011111110001', '1001110100100110111100010001110001000001011011111000010100001100100010000100000100010010101011111111100101001000000000110101010', '0011000111101111100001110000100010111011110010011011100101100001010100110111111110111011111000111001101001001001111100100110100', '1101111010001011011100010000000110000101000101011110011000011010100000001010100010011010100000101110110001100011100001101100000', '0001010111001011000010001111010001100100010010000010001110111110101000011100000100100001011000011111110100100011001000000110010']\n"
|
||
]
|
||
}
|
||
],
|
||
"source": [
|
||
"# second job\n",
|
||
"print(\n",
|
||
" \" > The first ten measurement results of job 2:\",\n",
|
||
" another_result[0].data.meas.get_bitstrings()[:10],\n",
|
||
")"
|
||
]
|
||
},
|
||
{
|
||
"cell_type": "markdown",
|
||
"id": "9484b7b7-e474-4291-b3ee-ace64f7cf126",
|
||
"metadata": {},
|
||
"source": [
|
||
"## Next steps\n",
|
||
"\n",
|
||
"<Admonition type=\"tip\" title=\"Recommendations\">\n",
|
||
" - [Specify advanced runtime options.](runtime-options-overview)\n",
|
||
" - Practice with primitives by working through the [Cost function lesson](https://learning.quantum.ibm.com/course/variational-algorithm-design/cost-functions#primitives) in IBM Quantum Learning.\n",
|
||
" - Learn how to transpile locally in the [Transpile](./transpile/) section.\n",
|
||
" - Try the [Submit pre-transpiled circuits](https://learning.quantum.ibm.com/tutorial/submitting-user-transpiled-circuits-using-primitives) tutorial.\n",
|
||
" - Read [Migrate to V2 primitives](/migration-guides/v2-primitives).\n",
|
||
" - Understand the [Job limits](/guides/job-limits) when sending a job to an IBM® QPU.\n",
|
||
"</Admonition>"
|
||
]
|
||
}
|
||
],
|
||
"metadata": {
|
||
"description": "Practical examples of using primitives in Qiskit Runtime.",
|
||
"kernelspec": {
|
||
"display_name": "Python 3",
|
||
"language": "python",
|
||
"name": "python3"
|
||
},
|
||
"language_info": {
|
||
"codemirror_mode": {
|
||
"name": "ipython",
|
||
"version": 3
|
||
},
|
||
"file_extension": ".py",
|
||
"mimetype": "text/x-python",
|
||
"name": "python",
|
||
"nbconvert_exporter": "python",
|
||
"pygments_lexer": "ipython3",
|
||
"version": "3"
|
||
},
|
||
"title": "Primitives examples"
|
||
},
|
||
"nbformat": 4,
|
||
"nbformat_minor": 4
|
||
}
|