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

Make circuit drawers *not crash* on Expr nodes #10504

Merged
merged 2 commits into from
Jul 27, 2023

Conversation

jakelishman
Copy link
Member

Summary

This at least causes the circuit visualisers to not crash when encountering an Expr node, and instead emit a warning and make a best-effort attempt (except for LaTeX) to output something. We intend to extend the capabilities of these drawers in the future.

Details and comments

This is not an ideal situation, and I've not written any tests really because there's not really much to test here. The intent is just to improve the situation beyond a crash to a warning that something's amiss, and display something. We're hoping to add more complete support later.

This at least causes the circuit visualisers to not crash when
encountering an `Expr` node, and instead emit a warning and make a
best-effort attempt (except for LaTeX) to output _something_.  We intend
to extend the capabilities of these drawers in the future.
@jakelishman jakelishman added stable backport potential The bug might be minimal and/or import enough to be port to stable Changelog: None Do not include in changelog mod: visualization qiskit.visualization labels Jul 26, 2023
@jakelishman jakelishman added this to the 0.25.0 milestone Jul 26, 2023
@jakelishman jakelishman requested review from a team and nonhermitian as code owners July 26, 2023 14:49
@qiskit-bot
Copy link
Collaborator

One or more of the the following people are requested to review this:

@coveralls
Copy link

coveralls commented Jul 26, 2023

Pull Request Test Coverage Report for Build 5681519764

  • 13 of 59 (22.03%) changed or added relevant lines in 4 files are covered.
  • 143 unchanged lines in 14 files lost coverage.
  • Overall coverage decreased (-0.003%) to 85.902%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/visualization/circuit/latex.py 3 4 75.0%
qiskit/visualization/circuit/text.py 4 18 22.22%
qiskit/visualization/circuit/matplotlib.py 3 34 8.82%
Files with Coverage Reduction New Missed Lines %
qiskit/circuit/library/n_local/efficient_su2.py 1 94.74%
qiskit/qasm/init.py 2 80.0%
qiskit/visualization/circuit/matplotlib.py 2 50.0%
crates/qasm2/src/lex.rs 3 90.38%
qiskit/circuit/init.py 3 84.38%
qiskit/converters/init.py 3 73.08%
qiskit/pulse/library/waveform.py 3 93.75%
qiskit/circuit/library/n_local/two_local.py 5 87.8%
qiskit/transpiler/preset_passmanagers/plugin.py 5 92.06%
qiskit/circuit/library/n_local/qaoa_ansatz.py 9 88.89%
Totals Coverage Status
Change from base Build 5667924881: -0.003%
Covered Lines: 73063
Relevant Lines: 85054

💛 - Coveralls

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

Overall this LGTM, it seems straightforward to just pull the classical bits off the expression and draw the circuit as it would otherwise (with the exception of latex). My only question is about emitting the warning. It seems potentially a bit noisy for users of expression circuits to warn every time the function is called draw function is called on circuits with expr in them.

@@ -416,7 +417,10 @@ def _build_latex_array(self):
num_cols_op = 1
wire_list = [self._wire_map[qarg] for qarg in node.qargs if qarg in self._qubits]
if getattr(op, "condition", None):
self._add_condition(op, wire_list, column)
if isinstance(op.condition, expr.Expr):
warn("ignoring expression condition, which is not supported yet")
Copy link
Member

Choose a reason for hiding this comment

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

Do you think UserWarning is appropriate here? I guess we want people to see this, but I'm wondering if it'll be too noisy? Especially since it's not necessarily user actionable if they're using classical expressions.

Copy link
Member Author

Choose a reason for hiding this comment

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

It should only get shown once per drawing, so I think it's ok. It's particularly important to be visible in the LaTeX drawer, because it straight up skips drawing any condition. The text and MPL drawers both make an attempt to show the data dependencies still.

Copy link
Member

Choose a reason for hiding this comment

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

Maybe just remove it for mpl and text then and leave it in the docstring. For latex I agree it's not correct and we should warn.

Copy link
Member Author

Choose a reason for hiding this comment

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

Done in 076ec9c.

@jakelishman
Copy link
Member Author

tbf, if anything, I'd think it's the MPL and text drawers whose warnings might be too noisy, because they still produce sensible (but not necessarily the prettiest) output, so there's still valid cause to use them. The LaTeX drawer produces technically invalid output, so I think the warning's most appropriate.

I'm not certain what would be better with the warnings, though - I'm not a big fan of a global "show warning if ..." filter since that takes away users' ability to control things, but maybe it's more appropriate here?

Copy link
Member

@mtreinish mtreinish left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for the fast update.

@mtreinish mtreinish enabled auto-merge July 27, 2023 15:49
@mtreinish mtreinish added this pull request to the merge queue Jul 27, 2023
Merged via the queue into Qiskit:main with commit c8552f6 Jul 27, 2023
13 checks passed
mergify bot pushed a commit that referenced this pull request Jul 27, 2023
* Make circuit drawers *not crash* on `Expr` nodes

This at least causes the circuit visualisers to not crash when
encountering an `Expr` node, and instead emit a warning and make a
best-effort attempt (except for LaTeX) to output _something_.  We intend
to extend the capabilities of these drawers in the future.

* Soften warnings about unsupported `Expr` nodes

(cherry picked from commit c8552f6)
@jakelishman jakelishman deleted the expr/no-crash-visualisation branch July 27, 2023 18:29
github-merge-queue bot pushed a commit that referenced this pull request Jul 27, 2023
* Make circuit drawers *not crash* on `Expr` nodes

This at least causes the circuit visualisers to not crash when
encountering an `Expr` node, and instead emit a warning and make a
best-effort attempt (except for LaTeX) to output _something_.  We intend
to extend the capabilities of these drawers in the future.

* Soften warnings about unsupported `Expr` nodes

(cherry picked from commit c8552f6)

Co-authored-by: Jake Lishman <jake.lishman@ibm.com>
to24toro pushed a commit to to24toro/qiskit-terra that referenced this pull request Aug 3, 2023
* Make circuit drawers *not crash* on `Expr` nodes

This at least causes the circuit visualisers to not crash when
encountering an `Expr` node, and instead emit a warning and make a
best-effort attempt (except for LaTeX) to output _something_.  We intend
to extend the capabilities of these drawers in the future.

* Soften warnings about unsupported `Expr` nodes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Changelog: None Do not include in changelog mod: visualization qiskit.visualization stable backport potential The bug might be minimal and/or import enough to be port to stable
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants