Add better support for MatrixGate in SVG output

Open
#8,598 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

This issue has not been assessed yet.

Description

kind/feature-request triage/discuss

Is your feature request related to a use case or problem? Please explain

This feature request is basically to implement the suggestion by @mpharrigan in #2313: handle MatrixGate in the SVG output module.

Describe the solution you would prefer

Here are some ideas for how to extend the code to do it:

  1. Indexed label + SVG <title> tooltip (recommended default): Render unnamed MatrixGates as U (or U₁, U₂, ... deduplicated by matrix value so repeated gates share an index; U₁[1], U₁[2] across multi-qubit wires) and attach the formatted matrix in an SVG <title> element for native browser/Jupyter hover inspection. Trade-off: keeps gate boxes uniform at any 2^n × 2^n size with zero JS, but exact entries are hidden in static PNG/PDF exports.

  2. Indexed label + optional legend below the circuit: Use the same U₁, U₂ labels on the wires and print each unique matrix in a compact footer block underneath the diagram (opt-in or auto-enabled for small matrices). Trade-off: preserves exact matrix values in static exports without distorting wire/column spacing, at the cost of extra vertical height and coupling circuit_to_svg (or a get_circuit_diagram_info hook) to pass matrix data.

  3. Size-capped inline matrix: Render small matrices (e.g., 2×2, optionally real/simple 4×4) directly inside the gate box using multi-line <tspan> elements and vector brackets, and fall back to option 1 (U₁ + <title>, or U(4×4)) for larger dimensions. Trade-off: stays consistent with to_text_diagram() for single-qubit gates and can be implemented inside tdd_to_svg directly from the multi-line text, but even a 2×2 complex matrix noticeably widens its column and tallens its row (the dynamic-spacing issue where PR #4630 stalled).

  4. Visual fingerprint glyph (magnitude/phase grid) + <title>: Draw a small 2^n × 2^n grid inside the gate box encoding entry magnitude (opacity or cell size) and phase (hue), with the numeric matrix in <title>. Trade-off: reveals sparsity and structure (diagonal, permutation, block-diagonal) inside a fixed square box, but is unreadable numerically without hover and departs from Cirq SVG's monochrome styling (accessibility and grayscale print concerns).

Option 1 works best as the baseline, optionally combined with option 2 (legend) or option 3 (inline 2×2 only) when static readability of entries matters. For tdd_to_svg specifically, note that Cirq's to_text_diagram_drawer already strips the MatrixGate object down to its multi-line string, so deduplicating by matrix or attaching a <title> requires either hashing the formatted matrix string from tdd or passing a custom get_circuit_diagram_info callback from circuit_to_svg.

How urgent is this for you? Is it blocking important work?

P2 – we should do it in the next couple of quarters

Dominant language
Python
Stars
5.1k
Forks
1.3k
Avg merge
1d 12h
Merged PRs (30d)
48

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from quantumlib/Cirq

All issues in quantumlib/Cirq

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.