Skip to content

Commit

Permalink
Fixing bug in qcstyle that modified custom style dict (#11874)
Browse files Browse the repository at this point in the history
* fixed qcstyle bug that modified style dict, added test

* black

* ensure that the keys in nested_attrs are not overwritten again

* release notes

* Update releasenotes/notes/qcstyle-bug-custom-style-dicts-22deab6c602ccd6a.yaml

---------

Co-authored-by: Julien Gacon <gaconju@gmail.com>
(cherry picked from commit d21127a)
  • Loading branch information
Durd3nT authored and mergify[bot] committed Feb 28, 2024
1 parent 61a56ab commit 7efaf15
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 9 deletions.
6 changes: 3 additions & 3 deletions qiskit/visualization/circuit/qcstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,11 @@ def update(self, other):
nested_attrs = {"displaycolor", "displaytext"}
for attr in nested_attrs.intersection(other.keys()):
if attr in self.keys():
self[attr].update(other.pop(attr))
self[attr].update(other[attr])
else:
self[attr] = other.pop(attr)
self[attr] = other[attr]

super().update(other)
super().update((key, value) for key, value in other.items() if key not in nested_attrs)


class DefaultStyle:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Fixed bug in :meth:`.QuantumCircuit.draw` that was causing custom style dictionaries
for the Matplotlib drawer to be modified upon execution.
25 changes: 19 additions & 6 deletions test/visual/mpl/circuit/test_circuit_matplotlib_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1010,15 +1010,16 @@ def test_user_style(self):
circuit.barrier(5, 6)
circuit.reset(5)

style = {
"name": "user_style",
"displaytext": {"H2": "H_2"},
"displaycolor": {"H2": ("#EEDD00", "#FF0000")},
}
fname = "user_style.png"
self.circuit_drawer(
circuit,
output="mpl",
style={
"name": "user_style",
"displaytext": {"H2": "H_2"},
"displaycolor": {"H2": ("#EEDD00", "#FF0000")},
},
style=style,
filename=fname,
)

Expand All @@ -1029,7 +1030,19 @@ def test_user_style(self):
FAILURE_DIFF_DIR,
FAILURE_PREFIX,
)
self.assertGreaterEqual(ratio, self.threshold)

with self.subTest(msg="check image"):
self.assertGreaterEqual(ratio, self.threshold)

with self.subTest(msg="check style dict unchanged"):
self.assertEqual(
style,
{
"name": "user_style",
"displaytext": {"H2": "H_2"},
"displaycolor": {"H2": ("#EEDD00", "#FF0000")},
},
)

def test_subfont_change(self):
"""Tests changing the subfont size"""
Expand Down

0 comments on commit 7efaf15

Please sign in to comment.