From eb20501fa61904c6fd066a460ce00aa79ac13414 Mon Sep 17 00:00:00 2001 From: --get-all Date: Fri, 24 Mar 2023 16:20:16 -0400 Subject: [PATCH 1/5] Ensure QuantumCircuit.metadata is always a dict This is for compatibility with https://github.com/Qiskit/qiskit-terra/pull/9849 --- qiskit_aer/backends/aerbackend.py | 6 ++---- test/terra/backends/aer_simulator/test_metadata.py | 2 +- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/qiskit_aer/backends/aerbackend.py b/qiskit_aer/backends/aerbackend.py index 4ff9a3d6ed..f75afb4c58 100644 --- a/qiskit_aer/backends/aerbackend.py +++ b/qiskit_aer/backends/aerbackend.py @@ -427,12 +427,10 @@ def _execute_circuits_job(self, circuits, noise_model, config, job_id="", format metadata_list = [] for idx, circ in enumerate(circuits): if circ.metadata: - metadata = circ.metadata + circ.metadata = {"metadata_index": idx} metadata_list.append(metadata) - circ.metadata = {} - circ.metadata["metadata_index"] = idx else: - metadata_list.append(None) + metadata_list.append({}) # Run simulation aer_circuits = assemble_circuits(circuits) diff --git a/test/terra/backends/aer_simulator/test_metadata.py b/test/terra/backends/aer_simulator/test_metadata.py index 28cbab20d4..e8f1fc62db 100644 --- a/test/terra/backends/aer_simulator/test_metadata.py +++ b/test/terra/backends/aer_simulator/test_metadata.py @@ -37,7 +37,7 @@ class TestMetadata(SimulatorTestCase): def test_single_circuit_metadata(self, method, device): """Test circuits with object metadata.""" backend = self.backend(method=method, device=device) - metadata = {1: object} + metadata = {1: object()} circuit = QuantumCircuit(1, name="circ0", metadata=metadata.copy()) result = backend.run(circuit).result() self.assertSuccess(result) From 3b405128aa0771e939d82f0a072cbd2014a63626 Mon Sep 17 00:00:00 2001 From: --get-all Date: Fri, 24 Mar 2023 16:27:00 -0400 Subject: [PATCH 2/5] fix order --- qiskit_aer/backends/aerbackend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit_aer/backends/aerbackend.py b/qiskit_aer/backends/aerbackend.py index f75afb4c58..252ad98552 100644 --- a/qiskit_aer/backends/aerbackend.py +++ b/qiskit_aer/backends/aerbackend.py @@ -427,8 +427,8 @@ def _execute_circuits_job(self, circuits, noise_model, config, job_id="", format metadata_list = [] for idx, circ in enumerate(circuits): if circ.metadata: - circ.metadata = {"metadata_index": idx} metadata_list.append(metadata) + circ.metadata = {"metadata_index": idx} else: metadata_list.append({}) From 5c5e0a9bf983f630f00d3a9d1e78e2dc2102ce4e Mon Sep 17 00:00:00 2001 From: --get-all Date: Fri, 24 Mar 2023 16:37:03 -0400 Subject: [PATCH 3/5] fix typo :( --- qiskit_aer/backends/aerbackend.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/qiskit_aer/backends/aerbackend.py b/qiskit_aer/backends/aerbackend.py index 252ad98552..bc9ee7f980 100644 --- a/qiskit_aer/backends/aerbackend.py +++ b/qiskit_aer/backends/aerbackend.py @@ -427,7 +427,7 @@ def _execute_circuits_job(self, circuits, noise_model, config, job_id="", format metadata_list = [] for idx, circ in enumerate(circuits): if circ.metadata: - metadata_list.append(metadata) + metadata_list.append(circ.metadata) circ.metadata = {"metadata_index": idx} else: metadata_list.append({}) From 2e6141af1217143e2052a9bb30d284c05908b486 Mon Sep 17 00:00:00 2001 From: --get-all Date: Mon, 27 Mar 2023 10:22:09 -0400 Subject: [PATCH 4/5] revert object->object() change --- test/terra/backends/aer_simulator/test_metadata.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/terra/backends/aer_simulator/test_metadata.py b/test/terra/backends/aer_simulator/test_metadata.py index e8f1fc62db..28cbab20d4 100644 --- a/test/terra/backends/aer_simulator/test_metadata.py +++ b/test/terra/backends/aer_simulator/test_metadata.py @@ -37,7 +37,7 @@ class TestMetadata(SimulatorTestCase): def test_single_circuit_metadata(self, method, device): """Test circuits with object metadata.""" backend = self.backend(method=method, device=device) - metadata = {1: object()} + metadata = {1: object} circuit = QuantumCircuit(1, name="circ0", metadata=metadata.copy()) result = backend.run(circuit).result() self.assertSuccess(result) From 89e8ba9f6eb2e5e08c056118de372e288697fe1a Mon Sep 17 00:00:00 2001 From: --get-all Date: Mon, 27 Mar 2023 11:10:43 -0400 Subject: [PATCH 5/5] make backportable --- qiskit_aer/backends/aerbackend.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/qiskit_aer/backends/aerbackend.py b/qiskit_aer/backends/aerbackend.py index bc9ee7f980..06481ae144 100644 --- a/qiskit_aer/backends/aerbackend.py +++ b/qiskit_aer/backends/aerbackend.py @@ -426,11 +426,13 @@ def _execute_circuits_job(self, circuits, noise_model, config, job_id="", format # Take metadata from headers of experiments to work around JSON serialization error metadata_list = [] for idx, circ in enumerate(circuits): + metadata_list.append(circ.metadata) + # TODO: we test for True-like on purpose here to condition against both None and {}, + # which allows us to support versions of Terra before and after QuantumCircuit.metadata + # accepts None as a valid value. This logic should be revisited after terra>=0.24.0 is + # required. if circ.metadata: - metadata_list.append(circ.metadata) circ.metadata = {"metadata_index": idx} - else: - metadata_list.append({}) # Run simulation aer_circuits = assemble_circuits(circuits)