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

marginal_counts fails on qiskit Result structure #10385

Closed
eendebakpt opened this issue Jul 4, 2023 · 5 comments
Closed

marginal_counts fails on qiskit Result structure #10385

eendebakpt opened this issue Jul 4, 2023 · 5 comments
Labels
bug Something isn't working

Comments

@eendebakpt
Copy link
Contributor

Environment

  • Qiskit Terra version: 0.24.1
  • Python version: 3.11
  • Operating system: Windows

What is happening?

The marginalize_counts method fails on the output of the Aer simulator (a qiskit.result.result.Result structure). A minimal example is included below.

The error generated is:

Traceback (most recent call last):

  Cell In[7], line 12
    result = marginal_counts(result, [0])

  File C:\develop\env311\Lib\site-packages\qiskit\result\utils.py:67 in marginal_counts
    result = deepcopy(result)

...

  File C:\develop\Python311\Lib\copy.py:231 in _deepcopy_dict
    y[deepcopy(key, memo)] = deepcopy(value, memo)

  File C:\develop\Python311\Lib\copy.py:161 in deepcopy
    rv = reductor(4)

TypeError: cannot pickle 'qiskit_aer.backends.controller_wrappers.AerCircuit' object

How can we reproduce the issue?

from qiskit import QuantumCircuit
from qiskit.result.utils import marginal_counts
from qiskit_aer import Aer

qc = QuantumCircuit(2, 2)
qc.h(0)
qc.h(1)
qc.measure([0, 1], [0, 1])

backend = Aer.get_backend("aer_simulator")
result = backend.run(qc, shots=100, memory=True).result()
result = marginal_counts(result, [0])

What should happen?

This code worked in previous version of qiskit-terra. The marginalize_counts should work without error on results generated by qiskit.

Any suggestions?

No response

@eendebakpt eendebakpt added the bug Something isn't working label Jul 4, 2023
@Abhiraj-Shrotriya
Copy link
Contributor

Abhiraj-Shrotriya commented Jul 10, 2023

Calling marginalize_counts on result would not be correct since the result object holds a lot of things (for the code given by you) -

result = Result(backend_name='qasm_simulator', backend_version='0.12.1', qobj_id='', job_id='---------', success=True, results=[ExperimentResult(shots=1000, success=True, meas_level=2, data=ExperimentResultData(counts={'0x1': 255, '0x3': 239, '0x2': 267, '0x0': 239}), header=QobjExperimentHeader(creg_sizes=[['c', 2]], global_phase=0.0, memory_slots=2, metadata={}, n_qubits=2, name='circuit-239', qreg_sizes=[['q', 2]]), status=DONE, circuit=Circuit(qubit=2, num_memory=2, num_registers=0, ops={h[0],[],h[1],[],measure[0],[],measure[1],[]}, shots=100, seed=2825605101, global_phase_angle=0), seed_simulator=2825605101, metadata={'batched_shots_optimization': False, 'method': 'stabilizer', 'active_input_qubits': [0, 1], 'device': 'CPU', 'remapped_qubits': False, 'num_qubits': 2, 'num_clbits': 2, 'sample_measure_time': 0.004132, 'input_qubit_map': [[0, 0], [1, 1]], 'measure_sampling': True, 'noise': 'ideal', 'parallel_shots': 1, 'parallel_state_update': 16, 'fusion': {'enabled': False}}, time_taken=0.0060285)], date=----------, status=COMPLETED, header=None, metadata={'parallel_experiments': 1, 'omp_enabled': True, 'max_memory_mb': 16333, 'max_gpu_memory_mb': 0, 'num_processes_per_experiments': 1, 'mpi_rank': 0, 'num_mpi_processes': 1, 'time_taken_execute': 0.0061044}, time_taken=0.00600433349609375)

Your problem should be fixed if you call marginalize_counts on result.get_counts()

@eendebakpt
Copy link
Contributor Author

@Abhiraj-Shrotriya Using marginal_counts(result.get_counts()) indeed works. But this results in a dictionary, instead of a Result structure.

The signature for marginalize_counts is:

marginal_counts(
    result: Union[dict, qiskit.result.result.Result],
    indices: Optional[List[int]] = None,
    ...

So it seems reasonable to expect the marginal_counts to work in Results input structures.

@jakelishman
Copy link
Member

This was caused by a bug (of sorts) in Aer (see Qiskit/qiskit-aer#1845). If you're able to use Aer 0.11 temporarily until Aer 0.12.2 can be released, hopefully in the next week or two, that should work around the problem, I think.

@jakelishman
Copy link
Member

Oh, Aer 0.12.0 might be fine too - I forget exactly which version the issue appeared in.

@eendebakpt
Copy link
Contributor Author

The issue will be fixed in the next release of qiskit aer (confirmed by running from git with commit 6802355b7f3f721098d62255ea994ce6abda5ed3), closing this issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants