From 30f219abf42b3d66a3b922e0c42c8dd4d2326c5e Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Wed, 3 Apr 2024 18:16:23 +0900 Subject: [PATCH 01/19] Fixes for dependency issues --- qiskit_aer/backends/aer_compiler.py | 13 +++++++++---- qiskit_aer/backends/aer_simulator.py | 4 +++- qiskit_aer/backends/aerbackend.py | 10 +++++++++- qiskit_aer/primitives/estimator.py | 1 + qiskit_aer/primitives/sampler.py | 12 +++++++----- ..._dependency_issues_by0.14-da7f11cb29710f86.yaml | 14 ++++++++++++++ .../matrix_product_state/matrix_product_state.hpp | 4 ++-- src/simulators/sample_vector.hpp | 4 +--- src/simulators/stabilizer/stabilizer_state.hpp | 2 +- 9 files changed, 47 insertions(+), 17 deletions(-) create mode 100644 releasenotes/notes/fixes_dependency_issues_by0.14-da7f11cb29710f86.yaml diff --git a/qiskit_aer/backends/aer_compiler.py b/qiskit_aer/backends/aer_compiler.py index bbb2e18a25..22a604ac07 100644 --- a/qiskit_aer/backends/aer_compiler.py +++ b/qiskit_aer/backends/aer_compiler.py @@ -609,7 +609,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 @@ -691,6 +691,7 @@ def assemble_circuit(circuit: QuantumCircuit): is_conditional, conditional_reg, conditional_expr, + basis_gates, ) index_map.append(num_of_aer_ops - 1) @@ -796,6 +797,7 @@ def _assemble_op( is_conditional, conditional_reg, conditional_expr, + basis_gates, ): operation = inst.operation qubits = [qubit_indices[qubit] for qubit in inst.qubits] @@ -816,7 +818,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", @@ -825,6 +827,9 @@ def _assemble_op( }: aer_circ.gate(name, qubits, params, [], conditional_reg, aer_cond_expr, label if label else name) + elif basis_gates is not None and name in basis_gates: + aer_circ.gate(name, qubits, params, [], conditional_reg, aer_cond_expr, + label if label else name) elif name == "measure": if is_conditional: aer_circ.measure(qubits, clbits, clbits) @@ -927,7 +932,7 @@ def _assemble_op( return num_of_aer_ops -def assemble_circuits(circuits: List[QuantumCircuit]) -> List[AerCircuit]: +def assemble_circuits(circuits: List[QuantumCircuit], basis_gates=None) -> List[AerCircuit]: """converts a list of Qiskit circuits into circuits mapped AER::Circuit Args: @@ -951,5 +956,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) diff --git a/qiskit_aer/backends/aer_simulator.py b/qiskit_aer/backends/aer_simulator.py index 083fc5f9d2..958a10ba89 100644 --- a/qiskit_aer/backends/aer_simulator.py +++ b/qiskit_aer/backends/aer_simulator.py @@ -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 @@ -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__) @@ -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, " diff --git a/qiskit_aer/backends/aerbackend.py b/qiskit_aer/backends/aerbackend.py index bc95a45de6..c0581af24c 100644 --- a/qiskit_aer/backends/aerbackend.py +++ b/qiskit_aer/backends/aerbackend.py @@ -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() @@ -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 diff --git a/qiskit_aer/primitives/estimator.py b/qiskit_aer/primitives/estimator.py index d4f9f65790..3c0ba42473 100644 --- a/qiskit_aer/primitives/estimator.py +++ b/qiskit_aer/primitives/estimator.py @@ -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:]] diff --git a/qiskit_aer/primitives/sampler.py b/qiskit_aer/primitives/sampler.py index 7f06124289..25f4b08aab 100644 --- a/qiskit_aer/primitives/sampler.py +++ b/qiskit_aer/primitives/sampler.py @@ -191,11 +191,13 @@ 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, - ) + for circuit in circuits: + self._backend.set_max_qubits(circuit.num_qubits) + circuit = transpile( + circuit, + self._backend, + **self._transpile_options, + ) for i, circuit in zip(to_handle, circuits): self._transpiled_circuits[(i, is_shots_none)] = circuit diff --git a/releasenotes/notes/fixes_dependency_issues_by0.14-da7f11cb29710f86.yaml b/releasenotes/notes/fixes_dependency_issues_by0.14-da7f11cb29710f86.yaml new file mode 100644 index 0000000000..68fd40c631 --- /dev/null +++ b/releasenotes/notes/fixes_dependency_issues_by0.14-da7f11cb29710f86.yaml @@ -0,0 +1,14 @@ +--- +fixes: + - | + Fixes for dependency issues caused by release 0.14. + + - fixed sampling measure > 63 qubits + Fix for samplingVector.allocate() has issue for > 63 qubits + + - ccz was accepted when input backend does not have ccz + Use basis_gates in AerCompiler when AerBackend is made by from_backend + + - fixed smaller coupling map was generated in Primitives V1 + Setting number of qubits before transpile for Primitives V1 + diff --git a/src/simulators/matrix_product_state/matrix_product_state.hpp b/src/simulators/matrix_product_state/matrix_product_state.hpp index 60cba8195f..51df5dd27e 100644 --- a/src/simulators/matrix_product_state/matrix_product_state.hpp +++ b/src/simulators/matrix_product_state/matrix_product_state.hpp @@ -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(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; @@ -811,7 +811,7 @@ std::vector State::sample_measure_all(uint_t shots, #pragma omp parallel for if (getenv("PRL_PROB_MEAS")) for (int_t i = 0; i < static_cast(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; } diff --git a/src/simulators/sample_vector.hpp b/src/simulators/sample_vector.hpp index 36717bfc4f..8f9fe60ee4 100644 --- a/src/simulators/sample_vector.hpp +++ b/src/simulators/sample_vector.hpp @@ -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; } diff --git a/src/simulators/stabilizer/stabilizer_state.hpp b/src/simulators/stabilizer/stabilizer_state.hpp index e695411b61..cd0e18f678 100644 --- a/src/simulators/stabilizer/stabilizer_state.hpp +++ b/src/simulators/stabilizer/stabilizer_state.hpp @@ -519,7 +519,7 @@ std::vector State::sample_measure(const reg_t &qubits, auto qreg_cache = BaseState::qreg_; std::vector 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; From b82b5ed0aa3237236ee8f9db5f57919ed0b3a162 Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Wed, 3 Apr 2024 18:24:50 +0900 Subject: [PATCH 02/19] lint --- qiskit_aer/backends/aer_compiler.py | 1 + 1 file changed, 1 insertion(+) diff --git a/qiskit_aer/backends/aer_compiler.py b/qiskit_aer/backends/aer_compiler.py index 22a604ac07..afd0802227 100644 --- a/qiskit_aer/backends/aer_compiler.py +++ b/qiskit_aer/backends/aer_compiler.py @@ -937,6 +937,7 @@ def assemble_circuits(circuits: List[QuantumCircuit], basis_gates=None) -> List[ Args: circuits: circuit(s) to be converted + basis_gates: supported gates to be converted Returns: a list of circuits to be run on the Aer backends and From 27a9ac285cfadf835ceb4e2ad34af8940d9757a5 Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Wed, 3 Apr 2024 18:33:48 +0900 Subject: [PATCH 03/19] lint --- qiskit_aer/backends/aer_compiler.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/qiskit_aer/backends/aer_compiler.py b/qiskit_aer/backends/aer_compiler.py index afd0802227..bbc09be81b 100644 --- a/qiskit_aer/backends/aer_compiler.py +++ b/qiskit_aer/backends/aer_compiler.py @@ -932,12 +932,12 @@ def _assemble_op( return num_of_aer_ops -def assemble_circuits(circuits: List[QuantumCircuit], basis_gates=None) -> 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: supported gates to be converted + basis_gates (list): supported gates to be converted Returns: a list of circuits to be run on the Aer backends and From 6e47bbd8fe69af52c1270e40a15ec7ab84803aae Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Thu, 4 Apr 2024 10:13:58 +0900 Subject: [PATCH 04/19] lint --- qiskit_aer/backends/aer_compiler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit_aer/backends/aer_compiler.py b/qiskit_aer/backends/aer_compiler.py index bbc09be81b..f347eeff3b 100644 --- a/qiskit_aer/backends/aer_compiler.py +++ b/qiskit_aer/backends/aer_compiler.py @@ -932,7 +932,7 @@ def _assemble_op( return num_of_aer_ops -def assemble_circuits(circuits: List[QuantumCircuit], basis_gates: list=None) -> 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: From 275e563b43b33bea4aa295f25130c55fc689d7bd Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Thu, 4 Apr 2024 10:26:26 +0900 Subject: [PATCH 05/19] fix release note --- .../fixes_dependency_issues_by0.14-da7f11cb29710f86.yaml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/releasenotes/notes/fixes_dependency_issues_by0.14-da7f11cb29710f86.yaml b/releasenotes/notes/fixes_dependency_issues_by0.14-da7f11cb29710f86.yaml index 68fd40c631..6fdb0e8595 100644 --- a/releasenotes/notes/fixes_dependency_issues_by0.14-da7f11cb29710f86.yaml +++ b/releasenotes/notes/fixes_dependency_issues_by0.14-da7f11cb29710f86.yaml @@ -3,12 +3,9 @@ fixes: - | Fixes for dependency issues caused by release 0.14. - - fixed sampling measure > 63 qubits - Fix for samplingVector.allocate() has issue for > 63 qubits + Fix for issue in samplingVector.allocate() when > 63 qubits - - ccz was accepted when input backend does not have ccz Use basis_gates in AerCompiler when AerBackend is made by from_backend - - fixed smaller coupling map was generated in Primitives V1 - Setting number of qubits before transpile for Primitives V1 + Setting number of qubits before transpile for Primitives V1 (issue #2084) From 3de892173f006a2e0b1221a91080113090103c0d Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Thu, 4 Apr 2024 10:45:25 +0900 Subject: [PATCH 06/19] fix sampler --- qiskit_aer/primitives/sampler.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/qiskit_aer/primitives/sampler.py b/qiskit_aer/primitives/sampler.py index 25f4b08aab..628c10ff0d 100644 --- a/qiskit_aer/primitives/sampler.py +++ b/qiskit_aer/primitives/sampler.py @@ -191,10 +191,10 @@ 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: - for circuit in circuits: + for i in len(circuits): self._backend.set_max_qubits(circuit.num_qubits) - circuit = transpile( - circuit, + circuits[i] = transpile( + circuits[i], self._backend, **self._transpile_options, ) From beb7c16f8fd13b835957f059d8b5038b53630c29 Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Thu, 4 Apr 2024 10:49:11 +0900 Subject: [PATCH 07/19] fix sampler --- qiskit_aer/primitives/sampler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit_aer/primitives/sampler.py b/qiskit_aer/primitives/sampler.py index 628c10ff0d..caaa7a0cef 100644 --- a/qiskit_aer/primitives/sampler.py +++ b/qiskit_aer/primitives/sampler.py @@ -192,7 +192,7 @@ def _transpile(self, circuit_indices: Sequence[int], is_shots_none: bool): circuits = (self._preprocess_circuit(circ) for circ in circuits) if not self._skip_transpilation: for i in len(circuits): - self._backend.set_max_qubits(circuit.num_qubits) + self._backend.set_max_qubits(circuits[i].num_qubits) circuits[i] = transpile( circuits[i], self._backend, From 4873a72c3648b1923c62b2f4783dc7ab628ca9b3 Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Thu, 4 Apr 2024 14:20:19 +0900 Subject: [PATCH 08/19] fix sampler --- qiskit_aer/backends/aer_compiler.py | 6 +++--- qiskit_aer/primitives/sampler.py | 17 ++++++++++------- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/qiskit_aer/backends/aer_compiler.py b/qiskit_aer/backends/aer_compiler.py index f347eeff3b..ed3e4af280 100644 --- a/qiskit_aer/backends/aer_compiler.py +++ b/qiskit_aer/backends/aer_compiler.py @@ -827,9 +827,6 @@ def _assemble_op( }: aer_circ.gate(name, qubits, params, [], conditional_reg, aer_cond_expr, label if label else name) - elif basis_gates is not None and name in basis_gates: - aer_circ.gate(name, qubits, params, [], conditional_reg, aer_cond_expr, - label if label else name) elif name == "measure": if is_conditional: aer_circ.measure(qubits, clbits, clbits) @@ -921,6 +918,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: + 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}" diff --git a/qiskit_aer/primitives/sampler.py b/qiskit_aer/primitives/sampler.py index caaa7a0cef..fa3bff71fd 100644 --- a/qiskit_aer/primitives/sampler.py +++ b/qiskit_aer/primitives/sampler.py @@ -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 @@ -191,13 +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: - for i in len(circuits): - self._backend.set_max_qubits(circuits[i].num_qubits) - circuits[i] = transpile( - circuits[i], - 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 From 8ff089c6574876c9faf6a708f4f617662bb644e4 Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Thu, 4 Apr 2024 14:24:29 +0900 Subject: [PATCH 09/19] fix sampler --- qiskit_aer/primitives/sampler.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit_aer/primitives/sampler.py b/qiskit_aer/primitives/sampler.py index fa3bff71fd..17350f0707 100644 --- a/qiskit_aer/primitives/sampler.py +++ b/qiskit_aer/primitives/sampler.py @@ -200,7 +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 = (self_._transpile_circuit(circ) for circ in circuits) + 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 From 60f53330a1aae74b718c28fb6c62c7b5f5d0d4bb Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Thu, 4 Apr 2024 14:45:55 +0900 Subject: [PATCH 10/19] remove skip cp38 --- pyproject.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyproject.toml b/pyproject.toml index 4d08ae9241..f6b8371875 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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 From 39aaab817200ab458fc5f6c5f299b3e82f17ace5 Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Fri, 5 Apr 2024 15:45:36 +0900 Subject: [PATCH 11/19] hide primitives V2 for qiskit < 1.0 --- qiskit_aer/primitives/__init__.py | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/qiskit_aer/primitives/__init__.py b/qiskit_aer/primitives/__init__.py index 7ed7414f0c..e94f0c9919 100644 --- a/qiskit_aer/primitives/__init__.py +++ b/qiskit_aer/primitives/__init__.py @@ -32,6 +32,8 @@ """ 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 From e8bd2703ea758e49b8359b6ee0014614ab3e5b89 Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Fri, 5 Apr 2024 15:48:25 +0900 Subject: [PATCH 12/19] lint --- qiskit_aer/primitives/__init__.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/qiskit_aer/primitives/__init__.py b/qiskit_aer/primitives/__init__.py index e94f0c9919..84a2d064d7 100644 --- a/qiskit_aer/primitives/__init__.py +++ b/qiskit_aer/primitives/__init__.py @@ -31,6 +31,8 @@ Estimator """ +import qiskit + from .estimator import Estimator from .sampler import Sampler From 13fc1fbe486fbd5a6ac2e345f9e336b09107930b Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Mon, 15 Apr 2024 13:48:21 +0900 Subject: [PATCH 13/19] add test case for sampling measure for large stabilizer circuit --- .../backends/aer_simulator/test_measure.py | 70 +++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/test/terra/backends/aer_simulator/test_measure.py b/test/terra/backends/aer_simulator/test_measure.py index eda06dbf7c..d3677a2bb5 100644 --- a/test/terra/backends/aer_simulator/test_measure.py +++ b/test/terra/backends/aer_simulator/test_measure.py @@ -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 @@ -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, + "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 # --------------------------------------------------------------------- From ca534ed69e9149dbd38236f7731efd7ec335f35c Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Mon, 15 Apr 2024 17:59:59 +0900 Subject: [PATCH 14/19] reduce warning --- src/simulators/sample_vector.hpp | 1 - 1 file changed, 1 deletion(-) diff --git a/src/simulators/sample_vector.hpp b/src/simulators/sample_vector.hpp index 8f9fe60ee4..afc4658302 100644 --- a/src/simulators/sample_vector.hpp +++ b/src/simulators/sample_vector.hpp @@ -183,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; From 177f2f905578c4811a16cdc6cfa24d47e41e52bb Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Mon, 22 Apr 2024 17:06:26 +0900 Subject: [PATCH 15/19] replace test case for large stabilizer with GHZ circuit --- .../backends/aer_simulator/test_measure.py | 73 +++---------------- 1 file changed, 11 insertions(+), 62 deletions(-) diff --git a/test/terra/backends/aer_simulator/test_measure.py b/test/terra/backends/aer_simulator/test_measure.py index d3677a2bb5..e9bf31025b 100644 --- a/test/terra/backends/aer_simulator/test_measure.py +++ b/test/terra/backends/aer_simulator/test_measure.py @@ -267,73 +267,22 @@ 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): + def test_measure_sampling_large_ghz_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"]]) + shots = 1000 + delta = 0.05 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.h(0) + for q in range(1,num_qubits): + qc.cx(q-1,q) qc.measure_all() backend = self.backend(method=method) - result = backend.run( - transpile(qc, backend, optimization_level=0), shots=10, seed_simulator=12345 - ).result() + result = backend.run(qc, shots=shots).result() counts = result.get_counts() - - ref_counts = {} - if num_qubits == 65: - ref_counts.update( - { - "00000010101011110100110101101101101100010011000110011100101001010": 1, - "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) + targets = {} + targets['0'*num_qubits] = shots/2 + targets['1'*num_qubits] = shots/2 + self.assertDictAlmostEqual(counts, targets, delta=delta * shots) # --------------------------------------------------------------------- # Test MPS algorithms for measure From 92f378056b77d73837602f0c5fe16a53c6d3e103 Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Mon, 22 Apr 2024 17:43:52 +0900 Subject: [PATCH 16/19] format --- test/terra/backends/aer_simulator/test_measure.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/terra/backends/aer_simulator/test_measure.py b/test/terra/backends/aer_simulator/test_measure.py index e9bf31025b..6b88e35e3e 100644 --- a/test/terra/backends/aer_simulator/test_measure.py +++ b/test/terra/backends/aer_simulator/test_measure.py @@ -280,8 +280,8 @@ def test_measure_sampling_large_ghz_stabilizer(self, method, device, num_qubits) result = backend.run(qc, shots=shots).result() counts = result.get_counts() targets = {} - targets['0'*num_qubits] = shots/2 - targets['1'*num_qubits] = shots/2 + targets['0'*num_qubits] = shots / 2 + targets['1'*num_qubits] = shots / 2 self.assertDictAlmostEqual(counts, targets, delta=delta * shots) # --------------------------------------------------------------------- From ff1db0503211a7c4550bfb1ebd0abdc1e68e368c Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Mon, 22 Apr 2024 17:46:54 +0900 Subject: [PATCH 17/19] format --- test/terra/backends/aer_simulator/test_measure.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/test/terra/backends/aer_simulator/test_measure.py b/test/terra/backends/aer_simulator/test_measure.py index 6b88e35e3e..72356e5f89 100644 --- a/test/terra/backends/aer_simulator/test_measure.py +++ b/test/terra/backends/aer_simulator/test_measure.py @@ -273,15 +273,15 @@ def test_measure_sampling_large_ghz_stabilizer(self, method, device, num_qubits) delta = 0.05 qc = QuantumCircuit(num_qubits) qc.h(0) - for q in range(1,num_qubits): - qc.cx(q-1,q) + for q in range(1, num_qubits): + qc.cx(q - 1, q) qc.measure_all() backend = self.backend(method=method) result = backend.run(qc, shots=shots).result() counts = result.get_counts() targets = {} - targets['0'*num_qubits] = shots / 2 - targets['1'*num_qubits] = shots / 2 + targets["0" * num_qubits] = shots / 2 + targets["1" * num_qubits] = shots / 2 self.assertDictAlmostEqual(counts, targets, delta=delta * shots) # --------------------------------------------------------------------- From debc66c6f7a22bde1082239ef3172082c90422ab Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Mon, 22 Apr 2024 18:23:44 +0900 Subject: [PATCH 18/19] convert basis_gates from list to set --- qiskit_aer/backends/aer_compiler.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/qiskit_aer/backends/aer_compiler.py b/qiskit_aer/backends/aer_compiler.py index bac4c7645c..4412215b1b 100644 --- a/qiskit_aer/backends/aer_compiler.py +++ b/qiskit_aer/backends/aer_compiler.py @@ -953,5 +953,8 @@ def assemble_circuits(circuits: List[QuantumCircuit], basis_gates: list = None) # Generate AerCircuit from the input circuit aer_qc_list, idx_maps = assemble_circuits(circuits=[qc]) """ - aer_circuits, idx_maps = zip(*[assemble_circuit(circuit, basis_gates) for circuit in circuits]) + basis_gates_set = set(basis_gates) + aer_circuits, idx_maps = zip( + *[assemble_circuit(circuit, basis_gates_set) for circuit in circuits] + ) return list(aer_circuits), list(idx_maps) From 025953749ec9504e508265fd1d92398b6725836d Mon Sep 17 00:00:00 2001 From: Jun Doi Date: Mon, 22 Apr 2024 18:30:50 +0900 Subject: [PATCH 19/19] fix assemble_circuits --- qiskit_aer/backends/aer_compiler.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/qiskit_aer/backends/aer_compiler.py b/qiskit_aer/backends/aer_compiler.py index 4412215b1b..12758b4573 100644 --- a/qiskit_aer/backends/aer_compiler.py +++ b/qiskit_aer/backends/aer_compiler.py @@ -953,8 +953,11 @@ def assemble_circuits(circuits: List[QuantumCircuit], basis_gates: list = None) # Generate AerCircuit from the input circuit aer_qc_list, idx_maps = assemble_circuits(circuits=[qc]) """ - basis_gates_set = set(basis_gates) - aer_circuits, idx_maps = zip( - *[assemble_circuit(circuit, basis_gates_set) for circuit in circuits] - ) + if basis_gates is not None: + basis_gates_set = set(basis_gates) + aer_circuits, idx_maps = zip( + *[assemble_circuit(circuit, basis_gates_set) for circuit in circuits] + ) + else: + aer_circuits, idx_maps = zip(*[assemble_circuit(circuit) for circuit in circuits]) return list(aer_circuits), list(idx_maps)