Skip to content

Commit

Permalink
Merge branch 'main' into support_var_storage
Browse files Browse the repository at this point in the history
  • Loading branch information
hhorii authored Jun 15, 2024
2 parents 47f96c3 + fb91534 commit 5228ff8
Show file tree
Hide file tree
Showing 17 changed files with 425 additions and 357 deletions.
14 changes: 7 additions & 7 deletions docs/getting_started.rst
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ Here is a basic example:
.. code:: python
import qiskit
from qiskit_aer import AerSimulator
from qiskit_aer.primitives import SamplerV2
# Generate 3-qubit GHZ state
circ = qiskit.QuantumCircuit(3)
Expand All @@ -172,14 +172,14 @@ Here is a basic example:
circ.cx(1, 2)
circ.measure_all()
# Construct an ideal simulator
aersim = AerSimulator()
# Construct an ideal simulator with SamplerV2
sampler = SamplerV2()
job = sampler.run([circ], shots=128)
# Perform an ideal simulation
result_ideal = qiskit.execute(circ, aersim).result()
counts_ideal = result_ideal.get_counts(0)
result_ideal = job.result()
counts_ideal = result_ideal[0].data.meas.get_counts()
print('Counts(ideal):', counts_ideal)
# Counts(ideal): {'000': 493, '111': 531}
Ready to get going?...
======================
Expand All @@ -204,4 +204,4 @@ Ready to get going?...
.. Hiding - Indices and tables
:ref:`genindex`
:ref:`modindex`
:ref:`search`
:ref:`search`
12 changes: 5 additions & 7 deletions docs/tutorials/2_device_noise_simulation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,9 @@
},
"outputs": [],
"source": [
"from qiskit import IBMQ, transpile\n",
"from qiskit import QuantumCircuit\n",
"from qiskit import QuantumCircuit, transpile\n",
"from qiskit_aer import AerSimulator\n",
"from qiskit.tools.visualization import plot_histogram"
"from qiskit.visualization import plot_histogram"
]
},
{
Expand Down Expand Up @@ -64,7 +63,7 @@
},
"outputs": [],
"source": [
"from qiskit.providers.fake_provider import FakeVigo\n",
"from qiskit_ibm_runtime.fake_provider import FakeVigo\n",
"device_backend = FakeVigo()"
]
},
Expand Down Expand Up @@ -233,9 +232,8 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
"import qiskit\n",
"qiskit.__version__\n"
]
},
{
Expand Down
7 changes: 3 additions & 4 deletions docs/tutorials/3_building_noise_models.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
"from qiskit import QuantumCircuit, transpile\n",
"from qiskit.quantum_info import Kraus, SuperOp\n",
"from qiskit_aer import AerSimulator\n",
"from qiskit.tools.visualization import plot_histogram\n",
"from qiskit.visualization import plot_histogram\n",
"\n",
"# Import from Qiskit Aer noise module\n",
"from qiskit_aer.noise import (NoiseModel, QuantumError, ReadoutError, \n",
Expand Down Expand Up @@ -868,9 +868,8 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
"import qiskit\n",
"qiskit.__version__\n"
]
}
],
Expand Down
7 changes: 3 additions & 4 deletions docs/tutorials/4_custom_gate_noise.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"from qiskit_aer import AerSimulator\n",
"from qiskit_aer.noise import NoiseModel, amplitude_damping_error\n",
"\n",
"from qiskit.tools.visualization import plot_histogram"
"from qiskit.visualization import plot_histogram"
]
},
{
Expand Down Expand Up @@ -486,9 +486,8 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
"import qiskit\n",
"qiskit.__version__\n"
]
},
{
Expand Down
34 changes: 6 additions & 28 deletions docs/tutorials/5_noise_transformation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -317,41 +317,19 @@
"outputs": [
{
"data": {
"text/html": [
"<h3>Version Information</h3><table><tr><th>Qiskit Software</th><th>Version</th></tr><tr><td><code>qiskit</code></td><td>0.24.0.dev0+dba2eff</td></tr><tr><td><code>qiskit-aer</code></td><td>0.11.2</td></tr><tr><td><code>qiskit-ignis</code></td><td>0.7.1</td></tr><tr><td><code>qiskit-ibmq-provider</code></td><td>0.20.0</td></tr><tr><td><code>qiskit</code></td><td>0.41.0</td></tr><tr><th>System information</th></tr><tr><td>Python version</td><td>3.8.11</td></tr><tr><td>Python compiler</td><td>Clang 12.0.5 (clang-1205.0.22.11)</td></tr><tr><td>Python build</td><td>default, Jul 27 2021 10:46:38</td></tr><tr><td>OS</td><td>Darwin</td></tr><tr><td>CPUs</td><td>8</td></tr><tr><td>Memory (Gb)</td><td>64.0</td></tr><tr><td colspan='2'>Wed Feb 15 14:17:23 2023 JST</td></tr></table>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
"'1.1.0'"
]
},
"execution_count": 7,
"metadata": {},
"output_type": "display_data"
},
{
"data": {
"text/html": [
"<div style='width: 100%; background-color:#d5d9e0;padding-left: 10px; padding-bottom: 10px; padding-right: 10px; padding-top: 5px'><h3>This code is a part of Qiskit</h3><p>&copy; Copyright IBM 2017, 2023.</p><p>This code is licensed under the Apache License, Version 2.0. You may<br>obtain a copy of this license in the LICENSE.txt file in the root directory<br> of this source tree or at http://www.apache.org/licenses/LICENSE-2.0.<p>Any modifications or derivative works of this code must retain this<br>copyright notice, and modified files need to carry a notice indicating<br>that they have been altered from the originals.</p></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"metadata": {},
"output_type": "display_data"
"output_type": "execute_result"
}
],
"source": [
"import qiskit.tools.jupyter\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
"import qiskit\n",
"qiskit.__version__"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
Expand All @@ -370,7 +348,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.11"
"version": "3.12.3"
},
"varInspector": {
"cols": {
Expand Down
7 changes: 3 additions & 4 deletions docs/tutorials/6_extended_stabilizer_tutorial.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"source": [
"from qiskit import QuantumCircuit, transpile\n",
"from qiskit_aer import AerSimulator\n",
"from qiskit.tools.visualization import plot_histogram\n",
"from qiskit.visualization import plot_histogram\n",
"\n",
"import random"
]
Expand Down Expand Up @@ -479,9 +479,8 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
"import qiskit\n",
"qiskit.__version__\n"
]
},
{
Expand Down
5 changes: 2 additions & 3 deletions docs/tutorials/7_matrix_product_state_method.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,8 @@
}
],
"source": [
"import qiskit.tools.jupyter\n",
"%qiskit_version_table\n",
"%qiskit_copyright"
"import qiskit\n",
"qiskit.__version__\n"
]
},
{
Expand Down
61 changes: 48 additions & 13 deletions qiskit_aer/backends/aer_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,9 +279,15 @@ def _inline_for_loop_op(self, instruction, parent, bit_map):
inlined_body = self._inline_circuit(body, continue_label, break_label, inner_bit_map)
if loop_parameter is not None:
inlined_body = inlined_body.assign_parameters({loop_parameter: index})
# parent.append(inlined_body, qargs, cargs)
for inst in inlined_body:
parent.append(inst, qargs, cargs)
parent.append(
inst.replace(
qubits=[inner_bit_map[bit] for bit in inst.qubits],
clbits=[inner_bit_map[bit] for bit in inst.clbits],
),
qargs,
cargs,
)
parent.append(AerMark(continue_label, len(qargs), len(cargs)), qargs, cargs)

if inlined_body is not None:
Expand All @@ -299,17 +305,18 @@ def _inline_while_loop_op(self, instruction, parent, bit_map):
continue_label = f"{loop_name}_continue"
loop_start_label = f"{loop_name}_start"
break_label = f"{loop_name}_end"
inline_bit_map = {
inner: bit_map[outer]
for inner, outer in itertools.chain(
zip(body.qubits, instruction.qubits),
zip(body.clbits, instruction.clbits),
)
}
inlined_body = self._inline_circuit(
body,
continue_label,
break_label,
{
inner: bit_map[outer]
for inner, outer in itertools.chain(
zip(body.qubits, instruction.qubits),
zip(body.clbits, instruction.clbits),
)
},
inline_bit_map,
)
qargs = [bit_map[q] for q in instruction.qubits]
cargs = [bit_map[c] for c in instruction.clbits]
Expand All @@ -333,7 +340,14 @@ def _inline_while_loop_op(self, instruction, parent, bit_map):
parent.append(AerJump(break_label, len(qargs), len(mark_cargs)), qargs, mark_cargs)
parent.append(AerMark(loop_start_label, len(qargs), len(mark_cargs)), qargs, mark_cargs)
for inst in inlined_body:
parent.append(inst, qargs, cargs)
parent.append(
inst.replace(
qubits=[inline_bit_map[bit] for bit in inst.qubits],
clbits=[inline_bit_map[bit] for bit in inst.clbits],
),
qargs,
cargs,
)
parent.append(AerJump(continue_label, len(qargs), len(mark_cargs)), qargs, mark_cargs)
parent.append(AerMark(break_label, len(qargs), len(mark_cargs)), qargs, mark_cargs)

Expand Down Expand Up @@ -383,7 +397,14 @@ def _inline_if_else_op(self, instruction, continue_label, break_label, parent, b
parent.append(AerMark(if_true_label, len(qargs), len(mark_cargs)), qargs, mark_cargs)
child = self._inline_circuit(true_body, continue_label, break_label, true_bit_map)
for inst in child.data:
parent.append(inst, qargs, cargs)
parent.append(
inst.replace(
qubits=[true_bit_map[bit] for bit in inst.qubits],
clbits=[true_bit_map[bit] for bit in inst.clbits],
),
qargs,
cargs,
)

if false_body:
false_bit_map = {
Expand All @@ -397,7 +418,14 @@ def _inline_if_else_op(self, instruction, continue_label, break_label, parent, b
parent.append(AerMark(if_else_label, len(qargs), len(mark_cargs)), qargs, mark_cargs)
child = self._inline_circuit(false_body, continue_label, break_label, false_bit_map)
for inst in child.data:
parent.append(inst, qargs, cargs)
parent.append(
inst.replace(
qubits=[false_bit_map[bit] for bit in inst.qubits],
clbits=[false_bit_map[bit] for bit in inst.clbits],
),
qargs,
cargs,
)

parent.append(AerMark(if_end_label, len(qargs), len(mark_cargs)), qargs, mark_cargs)

Expand Down Expand Up @@ -482,7 +510,14 @@ def _inline_switch_case_op(self, instruction, continue_label, break_label, paren
case_data.body, continue_label, break_label, case_data.bit_map
)
for inst in child.data:
parent.append(inst, qargs, cargs)
parent.append(
inst.replace(
qubits=[case_data.bit_map[bit] for bit in inst.qubits],
clbits=[case_data.bit_map[bit] for bit in inst.clbits],
),
qargs,
cargs,
)
parent.append(AerJump(switch_end_label, len(qargs), len(mark_cargs)), qargs, mark_cargs)

parent.append(AerMark(switch_end_label, len(qargs), len(mark_cargs)), qargs, mark_cargs)
Expand Down
3 changes: 3 additions & 0 deletions qiskit_aer/primitives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@
Sampler
Estimator
SamplerV2
EstimatorV2
"""

import qiskit
Expand Down
12 changes: 6 additions & 6 deletions qiskit_aer/primitives/estimator_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import numpy as np
from qiskit.primitives.base import BaseEstimatorV2
from qiskit.primitives.containers import EstimatorPubLike, PrimitiveResult, PubResult
from qiskit.primitives.containers import DataBin, EstimatorPubLike, PrimitiveResult, PubResult
from qiskit.primitives.containers.estimator_pub import EstimatorPub
from qiskit.primitives.primitive_job import PrimitiveJob
from qiskit.quantum_info import Pauli
Expand Down Expand Up @@ -132,7 +132,6 @@ def _run_pub(self, pub: EstimatorPub) -> PubResult:
).result()

# calculate expectation values (evs) and standard errors (stds)
rng = np.random.default_rng(self.options.run_options.get("seed_simulator"))
flat_indices = list(param_indices.ravel())
evs = np.zeros_like(bc_param_ind, dtype=float)
stds = np.full(bc_param_ind.shape, precision)
Expand All @@ -143,10 +142,11 @@ def _run_pub(self, pub: EstimatorPub) -> PubResult:
expval = result.data(flat_index)[pauli]
evs[index] += expval * coeff
if precision > 0:
evs = rng.normal(evs, precision)
data_bin_cls = self._make_data_bin(pub)
data_bin = data_bin_cls(evs=evs, stds=stds)
rng = np.random.default_rng(self.options.run_options.get("seed_simulator"))
if not np.all(np.isreal(evs)):
raise ValueError("Given operator is not Hermitian and noise cannot be added.")
evs = rng.normal(evs, precision, evs.shape)
return PubResult(
data_bin,
DataBin(evs=evs, stds=stds, shape=evs.shape),
metadata={"target_precision": precision, "simulator_metadata": result.metadata},
)
Loading

0 comments on commit 5228ff8

Please sign in to comment.