qiskit icon indicating copy to clipboard operation
qiskit copied to clipboard

_repr_png_ in QuantumCircuit allows nice output when circuits are the last stmt in jupyter notebooks

Open 1ucian0 opened this issue 4 years ago • 7 comments

By extending QuantumCircuit with _ipython_display_ "drawing" circuits in jupyter notebooks is even more direct.

Screenshot 2021-01-27 at 10 44 03

Before, qc.draw('mpl') needs to be used. This PR chooses 'mpl' drawer if matplotlib is installed, 'text' otherwise.

1ucian0 avatar Jan 27 '21 09:01 1ucian0

This probably needs extra logic as you can easily hang an users system at large circuit sizes when auto drawing.

nonhermitian avatar Jan 27 '21 12:01 nonhermitian

Good point. Do you limiting this to a particular circuit depth would also help? For example, draw only when circuit depth is less than 30. Same for size. What a good limit would be?

1ucian0 avatar Jan 28 '21 07:01 1ucian0

In c72b492 I added limits. If self.width() > 70 or self.size() > 100 or self.depth() > 30, the output is the default one and does not try to draw it.

1ucian0 avatar Jan 28 '21 14:01 1ucian0

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Feb 09 '21 14:02 CLAassistant

Pull Request Test Coverage Report for Build 4840647508

  • 1 of 4 (25.0%) changed or added relevant lines in 1 file are covered.
  • 507 unchanged lines in 14 files lost coverage.
  • Overall coverage increased (+0.06%) to 85.924%

Changes Missing Coverage Covered Lines Changed/Added Lines %
qiskit/circuit/quantumcircuit.py 1 4 25.0%
<!-- Total: 1 4
Files with Coverage Reduction New Missed Lines %
crates/accelerate/src/sabre_swap/mod.rs 2 99.53%
crates/qasm2/src/lex.rs 3 90.38%
qiskit/transpiler/passes/scheduling/padding/base_padding.py 3 95.89%
qiskit/transpiler/passes/scheduling/base_scheduler.py 4 87.5%
qiskit/transpiler/passes/basis/basis_translator.py 5 97.76%
qiskit/transpiler/passes/scheduling/dynamical_decoupling.py 9 81.67%
qiskit/transpiler/passes/scheduling/padding/dynamical_decoupling.py 9 93.29%
qiskit/visualization/state_visualization.py 16 57.43%
qiskit/providers/backend_compat.py 18 86.63%
qiskit/providers/fake_provider/fake_backend.py 20 90.66%
<!-- Total: 507
Totals Coverage Status
Change from base Build 4791194653: 0.06%
Covered Lines: 71188
Relevant Lines: 82850

💛 - Coveralls

coveralls avatar Apr 25 '23 15:04 coveralls

Do you want to keep the size limitations too, and potentially decay to some more minimal return if so? You can return None in those cases - it should be caught by the rich display.

yes! done in 4fd2890127c01c3ff0dfa263622dabeedd1eae54 . Thanks!

1ucian0 avatar Apr 29 '23 20:04 1ucian0

This is nice for small circuits, but rendering large circuits with many gates can be very very slow. In that case it could be very inconvenient if the default is generating the picture. Would it be good to add some heuristic that checks the circuit size/number of operations and just prints the normal <QuantumCircuit at bla> if it exceeds that?

Cryoris avatar May 03 '23 12:05 Cryoris