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

Fixes for dependency issues caused by 0.14 release #2094

Merged
merged 23 commits into from
Apr 23, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ build-backend = "setuptools.build_meta"
[tool.cibuildwheel]
manylinux-x86_64-image = "manylinux2014"
manylinux-i686-image = "manylinux2014"
skip = "pp* cp36* cp37* cp38* *musllinux*"
skip = "pp* cp36* cp37* *musllinux*"
test-skip = "cp3*-win32 cp3*-manylinux_i686"
test-command = "python {project}/tools/verify_wheels.py"
# We need to use pre-built versions of Numpy and Scipy in the tests; they have a
Expand Down
14 changes: 10 additions & 4 deletions qiskit_aer/backends/aer_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -605,7 +605,7 @@ def generate_aer_config(
return config


def assemble_circuit(circuit: QuantumCircuit):
def assemble_circuit(circuit: QuantumCircuit, basis_gates=None):
"""assemble circuit object mapped to AER::Circuit"""

num_qubits = circuit.num_qubits
Expand Down Expand Up @@ -687,6 +687,7 @@ def assemble_circuit(circuit: QuantumCircuit):
is_conditional,
conditional_reg,
conditional_expr,
basis_gates,
)
index_map.append(num_of_aer_ops - 1)

Expand Down Expand Up @@ -792,6 +793,7 @@ def _assemble_op(
is_conditional,
conditional_reg,
conditional_expr,
basis_gates,
):
operation = inst.operation
qubits = [qubit_indices[qubit] for qubit in inst.qubits]
Expand All @@ -812,7 +814,7 @@ def _assemble_op(

num_of_aer_ops = 1
# fmt: off
if name in {
if basis_gates is None and name in {
"ccx", "ccz", "cp", "cswap", "csx", "cx", "cy", "cz", "delay", "ecr", "h",
"id", "mcp", "mcphase", "mcr", "mcrx", "mcry", "mcrz", "mcswap", "mcsx",
"mcu", "mcu1", "mcu2", "mcu3", "mcx", "mcx_gray", "mcy", "mcz", "p", "r",
Expand Down Expand Up @@ -912,6 +914,9 @@ def _assemble_op(
aer_circ.mark(qubits, params)
elif name == "qerror_loc":
aer_circ.set_qerror_loc(qubits, label if label else name, conditional_reg, aer_cond_expr)
elif basis_gates is not None and name in basis_gates:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Aer C++ library supports only the following gates natively.

"ccx", "ccz", "cp", "cswap", "csx", "cx", "cy", "cz", "delay", "ecr", "h",
"id", "mcp", "mcphase", "mcr", "mcrx", "mcry", "mcrz", "mcswap", "mcsx",
"mcu", "mcu1", "mcu2", "mcu3", "mcx", "mcx_gray", "mcy", "mcz", "p", "r"

If basis_gates is not subset of the above set, simulation will fail. I think it is better to check name is in the above set.

Also, I guess backend.configuration().basis_gate is list and not set. basis_gates should be set because this is one of hot loops.

I think that at the beginning of assemble_circuit(), basis_gates should be re-created as a set (while reducing measure, reset and etc). If None is passed, we can use a default set.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When AerSimulator is made by from_backend() given basis_gates does not contain all the gates Aer supports, Aer should return error

aer_circ.gate(name, qubits, params, [], conditional_reg, aer_cond_expr,
label if label else name)
elif name in ("for_loop", "while_loop", "if_else"):
raise AerError(
"control-flow instructions must be converted " f"to jump and mark instructions: {name}"
Expand All @@ -923,11 +928,12 @@ def _assemble_op(
return num_of_aer_ops


def assemble_circuits(circuits: List[QuantumCircuit]) -> List[AerCircuit]:
def assemble_circuits(circuits: List[QuantumCircuit], basis_gates: list = None) -> List[AerCircuit]:
"""converts a list of Qiskit circuits into circuits mapped AER::Circuit

Args:
circuits: circuit(s) to be converted
basis_gates (list): supported gates to be converted
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As I mentioned the above, list should be converted to set here.


Returns:
a list of circuits to be run on the Aer backends and
Expand All @@ -947,5 +953,5 @@ def assemble_circuits(circuits: List[QuantumCircuit]) -> List[AerCircuit]:
# Generate AerCircuit from the input circuit
aer_qc_list, idx_maps = assemble_circuits(circuits=[qc])
"""
aer_circuits, idx_maps = zip(*[assemble_circuit(circuit) for circuit in circuits])
aer_circuits, idx_maps = zip(*[assemble_circuit(circuit, basis_gates) for circuit in circuits])
return list(aer_circuits), list(idx_maps)
4 changes: 3 additions & 1 deletion qiskit_aer/backends/aer_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import copy
import logging
from qiskit.providers import convert_to_target
from qiskit.providers.options import Options
from qiskit.providers.models import QasmBackendConfiguration
from qiskit.providers.backend import BackendV2, BackendV1
Expand All @@ -33,6 +34,7 @@

# pylint: disable=import-error, no-name-in-module, abstract-method
from .controller_wrappers import aer_controller_execute
from .name_mapping import NAME_MAPPING

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -856,7 +858,7 @@ def from_backend(cls, backend, **options):
name = configuration.backend_name
configuration.backend_name = f"aer_simulator_from({name})"

target = None
target = convert_to_target(configuration, properties, None, NAME_MAPPING)
else:
raise TypeError(
"The backend argument requires a BackendV2 or BackendV1 object, "
Expand Down
10 changes: 9 additions & 1 deletion qiskit_aer/backends/aerbackend.py
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,11 @@ def target(self):
tgt._coupling_graph = self._coupling_map.graph.copy()
return tgt

def set_max_qubits(self, max_qubits):
"""Set maximun number of qubits to be used for this backend."""
if self._target is not None:
self._configuration.n_qubits = max_qubits

def clear_options(self):
"""Reset the simulator options to default values."""
self._options = self._default_options()
Expand Down Expand Up @@ -445,7 +450,10 @@ def _execute_circuits_job(
# Compile circuits
circuits, noise_model = self._compile(circuits, **run_options)

aer_circuits, idx_maps = assemble_circuits(circuits)
if self._target is not None:
aer_circuits, idx_maps = assemble_circuits(circuits, self.configuration().basis_gates)
else:
aer_circuits, idx_maps = assemble_circuits(circuits)
if parameter_binds:
run_options["parameterizations"] = self._convert_binds(
circuits, parameter_binds, idx_maps
Expand Down
8 changes: 6 additions & 2 deletions qiskit_aer/primitives/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,11 @@
Estimator
"""

import qiskit

from .estimator import Estimator
from .estimator_v2 import EstimatorV2
from .sampler import Sampler
from .sampler_v2 import SamplerV2

if not qiskit.__version__.startswith("0."):
from .estimator_v2 import EstimatorV2
from .sampler_v2 import SamplerV2
1 change: 1 addition & 0 deletions qiskit_aer/primitives/estimator.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,6 +532,7 @@ def _transpile_circuits(self, circuits):
circuit = self._circuits[i].copy()
circuit.measure_all()
num_qubits = circuit.num_qubits
self._backend.set_max_qubits(num_qubits)
circuit = self._transpile(circuit)
bit_map = {bit: index for index, bit in enumerate(circuit.qubits)}
layout = [bit_map[qr[0]] for _, qr, _ in circuit[-num_qubits:]]
Expand Down
15 changes: 10 additions & 5 deletions qiskit_aer/primitives/sampler.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ def _preprocess_circuit(circuit: QuantumCircuit):
circuit.save_probabilities_dict(qargs)
return circuit

def _transpile_circuit(self, circuit):
self._backend.set_max_qubits(circuit.num_qubits)
transpiled = transpile(
circuit,
self._backend,
**self._transpile_options,
)
return transpiled

def _transpile(self, circuit_indices: Sequence[int], is_shots_none: bool):
to_handle = [
i for i in set(circuit_indices) if (i, is_shots_none) not in self._transpiled_circuits
Expand All @@ -191,11 +200,7 @@ def _transpile(self, circuit_indices: Sequence[int], is_shots_none: bool):
if is_shots_none:
circuits = (self._preprocess_circuit(circ) for circ in circuits)
if not self._skip_transpilation:
circuits = transpile(
list(circuits),
self._backend,
**self._transpile_options,
)
circuits = (self._transpile_circuit(circ) for circ in circuits)
for i, circuit in zip(to_handle, circuits):
self._transpiled_circuits[(i, is_shots_none)] = circuit

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
fixes:
- |
Fixes for dependency issues caused by release 0.14.

Fix for issue in samplingVector.allocate() when > 63 qubits

Use basis_gates in AerCompiler when AerBackend is made by from_backend

Setting number of qubits before transpile for Primitives V1 (issue #2084)

4 changes: 2 additions & 2 deletions src/simulators/matrix_product_state/matrix_product_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ State::sample_measure_using_apply_measure(const reg_t &qubits, uint_t shots,
for (int_t i = 0; i < static_cast<int_t>(shots); i++) {
temp.initialize(qreg_);
auto single_result = temp.apply_measure_internal(qubits, rnds_list[i]);
all_samples[i] = single_result;
all_samples[i].from_vector(single_result);
}
}
return all_samples;
Expand All @@ -811,7 +811,7 @@ std::vector<SampleVector> State::sample_measure_all(uint_t shots,
#pragma omp parallel for if (getenv("PRL_PROB_MEAS"))
for (int_t i = 0; i < static_cast<int_t>(shots); i++) {
auto single_result = qreg_.sample_measure(shots, rng);
all_samples[i] = single_result;
all_samples[i].from_vector(single_result);
}
return all_samples;
}
Expand Down
5 changes: 1 addition & 4 deletions src/simulators/sample_vector.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,9 +123,7 @@ void SampleVector::allocate(uint_t n, uint_t base) {
elem_mask_ = (1ull << (elem_shift_bits_ + 1)) - 1;
vec_mask_ = (1ull << vec_shift_bits_) - 1;

uint_t size = n >> vec_shift_bits_;
if (size == 0)
size = 1;
uint_t size = (n + (REG_SIZE >> elem_shift_bits_) - 1) >> vec_shift_bits_;
bits_.resize(size, 0ull);
size_ = n;
}
Expand Down Expand Up @@ -185,7 +183,6 @@ void SampleVector::from_vector_with_map(const reg_t &src, const reg_t &map,
uint_t pos = 0;
uint_t n = REG_SIZE >> elem_shift_bits_;
for (uint_t i = 0; i < bits_.size(); i++) {
uint_t n = REG_SIZE;
uint_t val = 0;
if (n > size_ - pos)
n = size_ - pos;
Expand Down
2 changes: 1 addition & 1 deletion src/simulators/stabilizer/stabilizer_state.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -519,7 +519,7 @@ std::vector<SampleVector> State::sample_measure(const reg_t &qubits,
auto qreg_cache = BaseState::qreg_;
std::vector<SampleVector> samples(shots);
for (int_t ishot = 0; ishot < shots; ishot++) {
samples[ishot] = apply_measure_and_update(qubits, rng);
samples[ishot].from_vector(apply_measure_and_update(qubits, rng));
BaseState::qreg_ = qreg_cache; // restore pre-measurement data from cache
}
return samples;
Expand Down
70 changes: 70 additions & 0 deletions test/terra/backends/aer_simulator/test_measure.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from test.terra.reference import ref_measure
from qiskit import QuantumCircuit
from qiskit import transpile
import qiskit.quantum_info as qi
from qiskit_aer import AerSimulator
from qiskit_aer.noise import NoiseModel
from qiskit_aer.noise.errors import ReadoutError, depolarizing_error
Expand Down Expand Up @@ -265,6 +266,75 @@ def test_measure_stablizer_64bit(self, method, device):

self.assertDictAlmostEqual(output, targets, delta=delta * shots)

@supported_methods(["stabilizer"], [65, 127, 433])
def test_measure_sampling_large_stabilizer(self, method, device, num_qubits):
"""Test sampling measure for large stabilizer circuit"""
paulis = qi.PauliList([num_qubits * i for i in ["X", "Y", "Z"]])
qc = QuantumCircuit(num_qubits)
for pauli in paulis:
for i, p in enumerate(pauli):
if p == qi.Pauli("Y"):
qc.sdg(i)
qc.h(i)
elif p == qi.Pauli("X"):
qc.h(i)
qc.measure_all()
backend = self.backend(method=method)
result = backend.run(
transpile(qc, backend, optimization_level=0), shots=10, seed_simulator=12345
).result()
counts = result.get_counts()

ref_counts = {}
if num_qubits == 65:
ref_counts.update(
{
"00000010101011110100110101101101101100010011000110011100101001010": 1,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to test whether stabilizer can work for large-qubit circuits, we can use GHZ and then validate result with more simpler counts.

"00110001000010110100000001111110011100110111000000110000010011100": 1,
"10100010101000011000101011111100101010011101000011010111111101011": 1,
"10000011001110111110110001111001100001010111100000001000110011110": 1,
"00011100011000110011001001000011110000010101000111000101111110110": 1,
"00000100000101110111010011011110001101100111111100001001100101011": 1,
"10101011100110001001001100000101000001000010100101001000100010111": 1,
"10000110100111001010101010011100010011110001011001110010100010100": 1,
"00010011011100111010110000100101111101001011110001100110101101000": 1,
"10100100011111100000100111011100101110001100100000111111111111011": 1,
}
)
elif num_qubits == 127:
ref_counts.update(
{
"0001010101111010011010110110110110001001100011001110010100101000110001000010110100000001111110011100110111000000110000010011100": 1,
"0010111011101001101111000110110011111110000100110010101100011100011000110011001001000011110000010101000111000101111110110100000": 1,
"0000010011101110010111000110010000011111111111101100010011011100111010110000100101111101001011110001100110101101000101010111001": 1,
"1100100000010000100001000010101101101100010110010100001001101101101111110011111001000111011011010010110111100000101001000111111": 1,
"1000100100110000010100000100001010010100100010001011110000110100111001010101010011100010011110001011001110010100010100000001000": 1,
"0000101010110000000011000000100011011100100101100001111101100100011011111111000001011100110111110100101001110000010110101010101": 1,
"0001010110001001011010110001101100100001101100011010011110011101111110001110101110111000000011001000100011001111100000111110100": 1,
"1011101111011100010101110011011111010110011011001001011000111101101100111001000010110010011001100010010111101011110110011011001": 1,
"1100111011111011000111100110000101011110000000100011001111010100010101000011000101011111100101010011101000011010111111101011000": 1,
"0000000000000001101000100001111100000010101001010011001011001101101000110111100110111101111110111100111101000011110110011000110": 1,
}
)
elif num_qubits == 433:
ref_counts.update(
{
"1010011100010011110001011001110010100010100000001000001011101110100110111100011011001111111000010011001010110001110001100011001100100100001111000001010100011100010111111011010000011001110111110110001111001100001010111100000001000110011110101000101010000110001010111111001010100111010000110101111111010110000001010101111010011010110110110110001001100011001110010100101000110001000010110100000001111110011100110111000000110000010011100": 1,
"0000010001101110010010110000111110110010001101111111100000101110011011111010010100111000001011010101010111001000000100001000010000101011011011000101100101000010011011011011111100111110010001110110110100101101111000001010010001111110000010011101110010111000110010000011111111111101100010011011100111010110000100101111101001011110001100110101101000101010111001100010010011000001010000010000101001010010001000101111000011010011100101010": 1,
"1001010110100111010001000101010000000000111101001100100101011010110111111101010000111101011001001100001110101111101011010011101000111110011100010100111000010000100001100000010110100001111101010000100000100110011101100010000000011110010000100100000111001111010101011110010000011001000101001110100011010100110101111000010001000110001110010011111010101011011000101010110001001110111100001100010101001011000101101110010100001000111010011": 1,
"0011000011110000001010100100010101001111000000100101011000101000010010110100111100101000100100101111101010011110110110011010000010110111110111000000110111111101101000100100101101000001100100010011101011010011111001010100111110000111100111110110010110100101000110110111010001010000001011000100010000000000100101111010101110001001100110011010110000111111101101010011000011011010111110111000101110001001001101110001010111110010001000111": 1,
"0110010101110010011110110111100111111100011111010010001000010101011111101001010011110000011001100111010111000111011010000011011011100101111010111101000010010011111011000011000000111000010101001101101110100100001100010011000001110011011011111000110100100110010100011000110111101010111000101101100101111010101110010010000000111101110000101010101100011101110110000001101100111010110110011000111010101111100001111010000010111000010011000": 1,
"0100111000011011011111101100100000000000000011010001000011111000000101010010100110010110011011010001101111001101111011111101111001111010000111101100110001101011101111011100010101110011011111010110011011001001011000111101101100111001000010110010011001100010010111101011110110011011001000101011000100101101011000110110010000110110001101001111001110111111000111010111011100000001100100010001100111110000011111010000001010101100000000110": 1,
"1101001011100010000010110000111110110011011111000110111111101110010101101001101110011110011101101000101101110010101010111010101100001110111011101001101110010001101111000101110001000110110100010101010101110101100010001010110011010110011100011010010010100100010100111101101101010100101011111010111010110001011100000010000000101000000000010011000100100000100101100011011100110000101010011110101000101110101010101010101101110000010011011": 1,
"0111111110110101111001101110001111101111000110110010110000101111100101010100100010001010110111100110010111000010000101001110011011111010011111100000001010101101010000110110001000001000111101110101101100100100010100010011101001000010001101111101000010000101000010001001000101011011011011101010101111100110000111000100011010110000100011000000000010101011111110011111010010110101100100111001000000100011100000000111101111001101100011101": 1,
"1111000111000110110000010111101110110010110001110110010001110010011110110111111011010110111110011010010011010110010010010011001001001100000000100110010101110100011011100001010010011100001011000000010000010010110101001000010100111011000001011000101111110100011000100011010001101000111111110011110010100010011011100100100011010100110111010101100000101110110111100111101001111000101100110101011011010000111101111001011110101011011000011": 1,
"0100100100011111111001111110100110001100101000011110011010001110001101100111111010010011110010000110011001010001001110110000111011000010000000011111011100001001010001110110111001011100001110101000101101011011010110110011110101111010111101011010000110101010111100110100101101011110100000100000110011100010011001111000010101010000011111001010010100001001101100001010101000101001100000001001110011010011000001011101011110010000110000111": 1,
}
)

for count in counts:
self.assertIn(count, ref_counts)

# ---------------------------------------------------------------------
# Test MPS algorithms for measure
# ---------------------------------------------------------------------
Expand Down
Loading