_repr_png_ in QuantumCircuit allows nice output when circuits are the last stmt in jupyter notebooks
By extending QuantumCircuit with _ipython_display_ "drawing" circuits in jupyter notebooks is even more direct.
Before, qc.draw('mpl') needs to be used. This PR chooses 'mpl' drawer if matplotlib is installed, 'text' otherwise.
This probably needs extra logic as you can easily hang an users system at large circuit sizes when auto drawing.
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?
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.
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 | |
|---|---|
| Change from base Build 4791194653: | 0.06% |
| Covered Lines: | 71188 |
| Relevant Lines: | 82850 |
💛 - 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!
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?