Skip to content

Commit

Permalink
Add test case to serialize result of simulation (#1869)
Browse files Browse the repository at this point in the history
* add test case to serialize result of simulation

* add pickle comparison

---------

Co-authored-by: Jun Doi <doichan@jp.ibm.com>
  • Loading branch information
hhorii and doichanj authored Oct 10, 2023
1 parent 94377f8 commit 64a9696
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions test/terra/backends/aer_simulator/test_executors.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@
AerSimualtor options tests
"""
import logging
import json
from math import ceil
import concurrent.futures
import pickle
import tempfile

from ddt import ddt
from qiskit import QuantumCircuit, transpile
from qiskit.circuit.random import random_circuit
from qiskit.circuit.library import QuantumVolume
from qiskit.quantum_info import Statevector
from qiskit_aer.noise.noise_model import AerJSONEncoder
from test.terra.reference import ref_kraus_noise
from qiskit_aer.jobs import AerJob, AerJobSet
from test.terra.backends.simulator_test_case import SimulatorTestCase, supported_methods
Expand Down Expand Up @@ -56,6 +61,32 @@ def run_random_circuits(backend, shots=None, **run_options):
return result, circuits, targets


class TestResultSerialization(SimulatorTestCase):
"""Test seriallization of AerJob"""

def test_aer_job_json_dump(self):
circuit = QuantumVolume(4, seed=111)
circuit.measure_all()
backend = self.backend(method="statevector")
result = backend.run(transpile(circuit, backend)).result()
data = json.dumps(result, cls=AerJSONEncoder)
result_copy = json.loads(data)
self.compare_counts(result, [circuit], [result_copy["results"][0]["data"]["counts"]])

def test_aer_job_picklable(self):
circuit = QuantumVolume(4, seed=111)
circuit.measure_all()
backend = self.backend(method="statevector")
result = backend.run(transpile(circuit, backend)).result()

with tempfile.TemporaryFile() as f:
pickle.dump(result, f)
f.seek(0)
result_copy = pickle.load(f)

self.assertEqual(result.get_counts(), result_copy.get_counts())


class CBFixture(SimulatorTestCase):
"""Extension tests for Aerbackend with cluster backend"""

Expand Down

0 comments on commit 64a9696

Please sign in to comment.