Skip to content

Consider using canonicalize consistently before circuit conversions #579

@GiggleLiu

Description

@GiggleLiu

Summary

Currently, different circuit conversion functions handle circuit simplification differently:

  • QASM compiler (qasm()): Now uses Optimise.canonicalize() upfront to recursively simplify the entire circuit before conversion
  • YaoToEinsum (yao2einsum()): Only calls Optimise.to_basictypes() as a fallback on individual unsupported blocks

Concern

This inconsistency can lead to:

  1. Different conversion functions failing on the same circuit for different reasons
  2. Harder to maintain - each converter has its own simplification logic
  3. Potential bugs when new block types are added (need to update multiple places)

Proposal

Consider using Optimise.canonicalize() (or a similar standardized pre-processing step) consistently across all circuit conversion functions:

  • qasm() - ✅ already uses canonicalize
  • yao2einsum() - could benefit from upfront canonicalization
  • Any future converters

This would:

  1. Make conversions more robust and predictable
  2. Centralize simplification logic in one place
  3. Ensure all converters handle the same normalized circuit structure

Related

This issue was discovered while adding OpenQASM support (PR #578). The QASM compiler needed canonicalize to handle nested chains and convert gates like SqrtX, SqrtY, SqrtW to basic rotation gates.

Questions

  1. Should canonicalize be the standard pre-processing for all converters?
  2. Are there cases where we explicitly don't want canonicalization?
  3. Should we document this as a convention for future converters?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions