Skip to content

Commit

Permalink
Fix bug in QuantumError.to_dict (Qiskit#1420)
Browse files Browse the repository at this point in the history
  • Loading branch information
chriseclectic committed Jan 4, 2022
1 parent c4a97d4 commit cb17b3f
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 15 deletions.
13 changes: 11 additions & 2 deletions qiskit/providers/aer/noise/errors/quantum_error.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,12 +452,21 @@ def error_term(self, position):

def to_dict(self):
"""Return the current error as a dictionary."""
# Assemble noise circuits
instructions = []
for circ in self._circs:
circ_inst = []
for inst, qargs, _ in circ.data:
qobj_inst = inst.assemble()
qobj_inst.qubits = [circ.find_bit(q).index for q in qargs]
circ_inst.append(qobj_inst.to_dict())
instructions.append(circ_inst)
# Construct error dict
error = {
"type": "qerror",
"id": self.id,
"operations": [],
"instructions": [[op[0].assemble().to_dict() for op in circ.data]
for circ in self._circs],
"instructions": instructions,
"probabilities": list(self.probabilities)
}
return error
Expand Down
10 changes: 10 additions & 0 deletions releasenotes/notes/fix_qerror_to_dict-13a7683ac4adddd4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
---
fixes:
- |
Fixes a bug in :meth:`.QuantumError.to_dict` where N-qubit circuit
instructions where the assembled instruction always applied to
qubits ``[0, ..., N-1]`` rather than the instruction qubits. This
bug also affected device and fake backend noise models.
See `Issue 1415 <https://github.com/Qiskit/qiskit-aer/issues/1415>`__
for details.
52 changes: 39 additions & 13 deletions test/terra/backends/aer_simulator/test_noise.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,19 +223,45 @@ def test_kraus_circuit_noise(self, method, device):
"""Test simulation with Kraus quantum errors in circuit."""
backend = self.backend(method=method, device=device)
shots = 1000
error1 = noise.amplitude_damping_error(0.05)
error2 = error1.tensor(error1)
error0 = noise.amplitude_damping_error(0.05)
error1 = noise.amplitude_damping_error(0.15)
error01 = error1.tensor(error0)

qc = QuantumCircuit(2)
qc.h(0)
qc.append(error1, [0])
qc.cx(0, 1)
qc.append(error2, [0, 1])
target_probs = qi.DensityMatrix(qc).probabilities_dict()
# Target Circuit 0
tc0 = QuantumCircuit(2)
tc0.h(0)
tc0.append(qi.Kraus(error0), [0])
tc0.cx(0, 1)
tc0.append(qi.Kraus(error01), [0, 1])
target_probs0 = qi.DensityMatrix(tc0).probabilities_dict()

# Add measurement
qc.measure_all()
result = backend.run(qc, shots=shots).result()
# Sim circuit 0
qc0 = QuantumCircuit(2)
qc0.h(0)
qc0.append(error0, [0])
qc0.cx(0, 1)
qc0.append(error01, [0, 1])
qc0.measure_all()

# Target Circuit 1
tc1 = QuantumCircuit(2)
tc1.h(1)
tc1.append(qi.Kraus(error0), [1])
tc1.cx(1, 0)
tc1.append(qi.Kraus(error01), [1, 0])
target_probs1 = qi.DensityMatrix(tc1).probabilities_dict()

# Sim circuit 1
qc1 = QuantumCircuit(2)
qc1.h(1)
qc1.append(error0, [1])
qc1.cx(1, 0)
qc1.append(error01, [1, 0])
qc1.measure_all()

result = backend.run([qc0, qc1], shots=shots).result()
self.assertSuccess(result)
probs = {key: val / shots for key, val in result.get_counts(0).items()}
self.assertDictAlmostEqual(target_probs, probs, delta=0.1)
probs = [{key: val / shots for key, val in result.get_counts(i).items()}
for i in range(2)]
self.assertDictAlmostEqual(target_probs0, probs[0], delta=0.1)
self.assertDictAlmostEqual(target_probs1, probs[1], delta=0.1)

0 comments on commit cb17b3f

Please sign in to comment.