> For the complete documentation index, see [llms.txt](https://docs.sirraya.org/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.sirraya.org/examples/diagram_demo.md).

# diagram\_demo

Watch a circuit change shape as it moves through compilation, instead of just reading gate counts.

```bash
cargo run --example diagram_demo
```

## What it does

Builds a small 3-qubit circuit (`H`, `Cx`, `Rz`, `Swap`, `Measure`) and renders it three times — as the original source IR, after native `{Rz, Ry, Rzz}` decomposition, and after lowering to IBM's backend — using `Diagram::from_circuit` / `from_native` / `from_backend`, each producing ASCII art. It also writes an SVG rendering of the source circuit to disk.

```rust
println!("{}", Diagram::from_circuit(&c).to_ascii());
println!("{}", Diagram::from_native(&decompose(&c)).to_ascii());
let bc = lower(&c, Backend::IbmQ);
println!("{}", Diagram::from_backend(&bc).to_ascii());

let svg = Diagram::from_circuit(&c).to_svg();
std::fs::write("diagram.svg", &svg).expect("failed to write SVG file");
```

## Output

Three ASCII diagrams printed to the terminal, plus `diagram.svg` written to the working directory — usable directly in documentation or a paper figure, not just a debugging aid.

## Why it matters

Gate counts tell you *how much* changed; a diagram tells you *what* changed and *where*. Useful any time you're debugging why a rewrite pass did something unexpected, or when writing documentation that needs to show a circuit rather than describe it in prose.

## Related

* [`pipeline_end_to_end`](/examples/pipeline_end_to_end.md) — uses the same `Diagram` API on a larger, multi-stage pipeline
* [`routing_demo`](/examples/routing_demo.md) — shows a before/after diagram specifically to make SWAP insertion visible


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://docs.sirraya.org/examples/diagram_demo.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
