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

bug loading MPL style from the configuration #11750

Merged
merged 6 commits into from
Feb 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 1 addition & 2 deletions qiskit/visualization/circuit/qcstyle.py
Original file line number Diff line number Diff line change
Expand Up @@ -224,8 +224,7 @@ def load_style(style: dict | str | None) -> tuple[StyleDict, float]:
config_path = config.get("circuit_mpl_style_path", "")
if config_path:
for path in config_path:
path_ = Path(config_path) / style_name
style_paths.append(path_)
style_paths.append(Path(path) / style_name)

# check current directory
cwd_path = Path("") / style_name
Expand Down
4 changes: 4 additions & 0 deletions releasenotes/notes/fix_11536-c87d192a133b3dc3.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
fixes:
- |
A bug when loading MPL style from the configuration was fixed.
15 changes: 15 additions & 0 deletions test/python/visualization/test_circuit_drawer.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,21 @@ def test_default_output(self):
out = visualization.circuit_drawer(circuit)
self.assertIsInstance(out, text.TextDrawing)

@unittest.skipUnless(optionals.HAS_MATPLOTLIB, "Skipped because matplotlib is not available")
def test_mpl_config_with_path(self):
with patch(
"qiskit.user_config.get_config",
return_value={
"circuit_drawer": "mpl",
"circuit_mpl_style": "quantum-light",
"circuit_mpl_style_path": ["~/.qiskit"],
},
):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This particular form causes the test to generate a warning (we don't forbid UserWarning in CI) unless mthree is installed, which drops the quantum-light.json file into ~/.qiskit.

We maybe want to make the test resilient against that - perhaps use a temporary directory as the style_path that includes a valid style file we can use?

Copy link
Member Author

@1ucian0 1ucian0 Feb 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

true. Done in ae5f93c I ended up with a kinda complicated solution. Any ideas how to improve it?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think what you had was fine in principle - the interface of the circuit drawer is kind of missing a couple of things that would have made it a bit easier to do, so I think everything was always going to look a little messy. I tweaked it over to use TemporaryDirectory instead and to add a test of a failed lookup in 05eb769, but mostly just to expand the coverage more than anything else.

circuit = QuantumCircuit(1)
circuit.h(0)
out = visualization.circuit_drawer(circuit)
self.assertIsInstance(out, figure.Figure)

@unittest.skipUnless(optionals.HAS_MATPLOTLIB, "Skipped because matplotlib is not available")
def test_user_config_default_output(self):
with patch("qiskit.user_config.get_config", return_value={"circuit_drawer": "mpl"}):
Expand Down