Skip to content

Conversation

david-pl
Copy link
Collaborator

Still needs better testing, but works in principle.

I'm abusing EmptyLattice here a bit. Technically, I think you'd need a lattice to represent

  • Hermitian
  • NotHermitian
  • NotAnOperator

and similar for unitary.
I'm just doing this now with True, False and missing.

Copy link

codecov bot commented Aug 19, 2025

Copy link
Contributor

github-actions bot commented Aug 19, 2025

☂️ Python Coverage

current status: ✅

Overall Coverage

Lines Covered Coverage Threshold Status
10635 9372 88% 0% 🟢

New Files

File Coverage Status
src/bloqade/squin/analysis/hermitian/_init_.py 100% 🟢
src/bloqade/squin/analysis/hermitian/analysis.py 95% 🟢
src/bloqade/squin/analysis/hermitian/impls.py 91% 🟢
src/bloqade/squin/analysis/hermitian/lattice.py 100% 🟢
src/bloqade/squin/analysis/unitary/_init_.py 100% 🟢
src/bloqade/squin/analysis/unitary/analysis.py 91% 🟢
src/bloqade/squin/analysis/unitary/impls.py 79% 🟢
src/bloqade/squin/analysis/unitary/lattice.py 100% 🟢
TOTAL 95% 🟢

Modified Files

File Coverage Status
src/bloqade/squin/analysis/_init_.py 100% 🟢
src/bloqade/squin/groups.py 97% 🟢
src/bloqade/squin/op/stmts.py 99% 🟢
src/bloqade/squin/op/traits.py 67% 🟢
src/bloqade/squin/rewrite/_init_.py 100% 🟢
src/bloqade/squin/rewrite/canonicalize.py 97% 🟢
TOTAL 93% 🟢

updated for commit: cab82c7 by action🐍

@david-pl david-pl requested review from johnzl-777 and kaihsin August 20, 2025 08:05
@david-pl david-pl marked this pull request as ready for review August 20, 2025 08:05
Copy link
Contributor

@kaihsin kaihsin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to just make proper lattice not abusing EmptyLattice. For future extension

Copy link
Contributor

@johnzl-777 johnzl-777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! +1 on @kaihsin 's feedback. Just to plan ahead a bit, with the stuff I brought up in #468 the goal is not to knock that out here, right? Although this infrastructure is certainly necessary to do so!



@statement(dialect=dialect)
class Rot(CompositeOp):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a reason Rot doesn't have any hermitian trait?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No not really. I can add it, but it's only hermitian if the angle is 0, in which case it's actually just the identity. So the use-case is just very limited, which is why I skipped it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, this also applies to U3, PhaseOp and ShiftOp (maybe a few more). I feel like it's a bit of an overkill to have the hermitian analysis in place here, as it's always only the case that they are hermitian when the exponent is 0 and therefore the operator is just the identity. What do you think @johnzl-777 ?

As a side note: I generally think this should be a rewrite pass, which just replaces the operator by the identity directly. But we don't really have any such simplification passes in-place right now. For example, simplifying multiplications with identity operators would be another such candidate.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I think your proposal for a rewrite makes sense here instead of burdening other statements with the hermitian information. I suppose I'm just nervous how much of a downside we could get if it turns out something IS hermitian (this 0 exponent case) and we don't catch it.

I imagine it wouldn't be the worst thing in the world and if we knew we'd have something spitting out 0-exponents we'd most likely have thought to include that rewrite in the first place.

@david-pl
Copy link
Collaborator Author

@johnzl-777 thanks for the review!

Just to plan ahead a bit, with the stuff I brought up in #468 the goal is not to knock that out here, right?

Yes, my idea was to get this done first and then the rewrite for Adjoint could use this info, which should make the implementation of the rewrite quite simple, I hope.

@david-pl
Copy link
Collaborator Author

Okay, after the first round of review I've now added a proper lattice implementation for both analyses. They are just

  • NotHermitian (top)
  • Hermitian
  • NotAnOperator (bottom)

and similar for Unitary. I'm not sure if have NotHermitian as top is 100% correct in terms of semantics, but in the implementations you always reach that top when you can't decide whether an operator is hermitian, so treating it as NotHermitian is fine, I think. The rewrite pass that sets the attribute would behave exactly the same on NotHermitian and another top such as AnyHermitian.

Ready for another round of review!

@david-pl david-pl requested review from johnzl-777 and kaihsin August 27, 2025 09:20
@johnzl-777
Copy link
Contributor

and similar for Unitary. I'm not sure if have NotHermitian as top is 100% correct in terms of semantics, but in the implementations you always reach that top when you can't decide whether an operator is hermitian, so treating it as NotHermitian is fine, I think. The rewrite pass that sets the attribute would behave exactly the same on NotHermitian and another top such as AnyHermitian.

My understanding of the top element in the type lattice is there is the possibility it COULD be any other lattice element but there's insufficient information to narrow it down. In that case I think something like PossibleHermitian might be better here as well as PossibleUnitary

Copy link
Contributor

@johnzl-777 johnzl-777 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm still a bit hesitant to have a lattice top element have "Not" as part of its name and I also think it a bit odd to have a check for bool surrounding the scale impl (even though I just now learned it's literally subclassed from int)



@statement(dialect=dialect)
class Rot(CompositeOp):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, I think your proposal for a rewrite makes sense here instead of burdening other statements with the hermitian information. I suppose I'm just nervous how much of a downside we could get if it turns out something IS hermitian (this 0 exponent case) and we don't catch it.

I imagine it wouldn't be the worst thing in the world and if we knew we'd have something spitting out 0-exponents we'd most likely have thought to include that rewrite in the first place.

@david-pl
Copy link
Collaborator Author

I'm still a bit hesitant to have a lattice top element have "Not" as part of its name

@johnzl-777 I agree. While working out the solution, however, I noticed a problem, which I now realize is why I wrote things like this initially:

Any basic operator statement that is not hermitian or not unitary represents this by an absence of the respective trait. For example, SqrtX is unitary, but not hermitian. So in that sense, they are not really distinguishable from an operator for which we can't infer whether it is hermitian or not.

I solved this by adding a simple check for PrimitiveOp in the analysis fallback, which should be fine, I suppose, but it might miss a few things.

@david-pl david-pl requested a review from johnzl-777 September 15, 2025 09:43
@david-pl david-pl linked an issue Sep 15, 2025 that may be closed by this pull request
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Hermitian trait for squin operators

3 participants