quantumlib / Cirq

A Python framework for creating, editing, and invoking Noisy Intermediate Scale Quantum (NISQ) circuits.
Apache License 2.0
4.28k stars 1.02k forks source link

Short-circuit value equality with an identity check #6372

Closed maffoo closed 11 months ago

maffoo commented 11 months ago

We often reuse instances of objects with value semantics, and in such cases equality checking can be short-circuited by first checking for object identity before falling back to actually comparing the values of two objects. This is particular beneficial if we actually take care to reuse instances, which is something we should consider doing more generally to avoid object allocation and repeated computation of various properties such as hashes (see https://github.com/quantumlib/Cirq/pull/6371).