Skip to content

Easily count instructions in a circuit #1235

@sesmart

Description

@sesmart

Describe the feature you'd like

A simple method or tool for counting instructions.

How would this feature be used? Please describe.

Easily used for benchmarking, sanity-checks, or just gathering statistics on a Circuit. This would preferably be a Circuit class method, or an accumulating static property.

A nice parallel is with list.count()

target_circuits.count('cnot')

Describe alternatives you've considered

Like many things, you just manually iterate over instructions and pattern match. Requires knowledge of instruction attributes, and so on. This request is really just a convenience function.

def count_gates(circ)
    gates = {}
    for ins in circ.instructions:
        name = ins.operator.name.lower()
        gates[name] = gates.get(name,0) + 1
    return gates

Additional context
Some stretch ideas

  • Could also collect the density of operations
  • Could collect instructions per qubit

uH 2026

Please note that we're still waiting for Unitary Foundation to review and approve our issues, so these are subject to change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels
    No fields configured for Feature.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions