Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Counting rotations notes #1309

Closed
mpharrigan opened this issue Aug 20, 2024 · 20 comments
Closed

Counting rotations notes #1309

mpharrigan opened this issue Aug 20, 2024 · 20 comments

Comments

@mpharrigan
Copy link
Collaborator

No description provided.

@mpharrigan
Copy link
Collaborator Author

mpharrigan commented Aug 20, 2024

We treat Rz(pi/4) as a T gate. I'm not sure how I feel about that; but for expediency I will reproduce this behavior in the QECGatesCost cost.

However, the t_complexity protocol treats Rx(pi/4) as a rotation instead of a T and a clifford. I think we should be consistent and count these as a T gate. I'm not going to count them as a T+clifford because that complicates the code.

The Rx and Ry bloq examples use T angles. We should change them to be arbitrary angle

✅ Done in #1313

@mpharrigan
Copy link
Collaborator Author

mpharrigan commented Aug 20, 2024

LPRSInterimPrep -- its t_complexity method does not match its build_call_graph, or at least not how we usually treat it. Elsewhere, we treat a CRz as one "rotation"; here' each is treated as 2 rotations and 3 cliffords

def _t_complexity_(self) -> 'TComplexity':

✅ Done in #1323

@mpharrigan
Copy link
Collaborator Author

mpharrigan commented Aug 20, 2024

QvrZPow.qvr_zpow uses six ZPowGates. But the exponent is less than epsilon and my wip code treats it as six cliffords

🟢 no longer applicable; following comment in #1313 , only classical-machine-tolerance angles will get rounded to clifford

@mpharrigan mpharrigan added this to the v1.0 milestone Aug 20, 2024
@mpharrigan
Copy link
Collaborator Author

mpharrigan commented Aug 20, 2024

I think the t complexity of QuantumVariableRotation is very incorrect

return self.phi_bitsize * Rz(0.0).t_complexity()

since the 0 angle gets turned into a clifford

✅ done in #1323

@mpharrigan
Copy link
Collaborator Author

mpharrigan commented Aug 20, 2024

LPResourceState also relies on simplifying CZ**1.0 to CZ

✅ done in #1297

edit: I apparently wrote this down twice, but the second comment is more accurate: simplification is not necessary since it's always CZ

@mpharrigan
Copy link
Collaborator Author

mpharrigan commented Aug 20, 2024

return TComplexity(rotations=3)

SU2RotationGate always returns 3 rotations, no matter the angles; even if they turn into cliffords or Ts. Indeed the bloq_examples all use angles that can be specialized.

What do we want to do for this? @anurudhp

✅ fixed by #1288

@mpharrigan
Copy link
Collaborator Author

mpharrigan commented Aug 20, 2024

return TComplexity(t=4 * num_and, clifford=num_clifford)

bloq_example "cmodadd_symbolic" has CAdd as one of its callees. The t complexity protocol reports the number of toffolis as $8n+4$, which would be $2n + 1$ AND's. The call graph reports n AND + 1 Add with $n$ ands; giving a total of $2n$ ANDs

@NoureldinYosri can you investigate

✅ fixed by #1329

@mpharrigan
Copy link
Collaborator Author

mpharrigan commented Aug 20, 2024

https://github.com/quantumlib/Qualtran/blob/0f09271e6a45ed8acf020e0cce266fefbc0f6adb/qualtran/bloqs/phase_estimation/lp_resource_state.py#L171

LPResourceState uses CZPowGate(1.0) instead of just a CZ gate. Will change

✅ fixed by #1297

@tanujkhattar
Copy link
Collaborator

@mpharrigan The LPResourceState implementation is updated as part of #1297

@mpharrigan
Copy link
Collaborator Author

mpharrigan commented Aug 20, 2024

What should the cost be of the state and effect bloqs. MinusState etc. Allocate and Free have historically returned 0 TComplexity()

#1323 gives these as zero complexity

@mpharrigan
Copy link
Collaborator Author

There's a meaningful discrepancy in the rotation count in HamiltonianSimulationByGQSP. The GeneralizedQSP bloq call graph emits a lot of SU2 rotations, which are each treated as 3 rotations in the legacy protocol but can be simplified.

I would strongly advocate for changing the call graph method of GeneralizedQSP to give n SU2 rotations instead of listing all of the different ones. The call graph is pretty unreadable at present

@mpharrigan
Copy link
Collaborator Author

mpharrigan commented Aug 20, 2024

ApplyLthGate is blocked by #1207

✅ fixed by #1327

@NoureldinYosri
Copy link
Contributor

return TComplexity(t=4 * num_and, clifford=num_clifford)

bloq_example "cmodadd_symbolic" has CAdd as one of its callees. The t complexity protocol reports the number of toffolis as 8 n + 4 , which would be 2 n + 1 AND's. The call graph reports n AND + 1 Add with n ands; giving a total of 2 n ANDs

@NoureldinYosri can you investigate

fixed in #1329

@mpharrigan
Copy link
Collaborator Author

mpharrigan commented Aug 21, 2024

This is broken in t_complexity as well; maybe related to #1313.

GeneralizedQSP results in Controlled(XPowGate) with clifford angles that confuse the current (hacky) logic for Controlled(Rot). We need real CRx, CRy, CRz, CXPowGate, CYPowGate. We have CZPowGate

The hacky logic probably used to consider these rotations(??) which is also wrong (given our convention)

#1333

@mpharrigan
Copy link
Collaborator Author

mpharrigan commented Aug 21, 2024

scaled_chebyshev_poly_even is broken for t_complexity and gate counts because it runs into Controlled(atomic)

scaled_chebyshev_poly_even
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for ScaledChebyshevPolynomial from 1 callee(s)
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for LinearCombination from 9 callee(s)
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for Adjoint(subbloq=BlackBoxPrepare) from 1 callee(s)
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for Adjoint(subbloq=AutoPartition) from 5 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for Partition in 2.9667e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for Partition in 4.8083e-05 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for Adjoint(subbloq=StatePreparationAliasSampling) from 5 callee(s)
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for Adjoint(subbloq=PrepareUniformSuperposition) from 3 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for Split in 4.15e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for H in 1.6667e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for Join in 3.2791e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for Adjoint(subbloq=PrepareUniformSuperposition) in 0.00243521 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for LessThanEqual from 6 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for XGate in 1.4583e-05 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for SingleQubitCompare from 3 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for CNOT in 2.1042e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for And in 1.0459e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for SingleQubitCompare in 0.000872834 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for BiQubitsMixer from 3 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for BiQubitsMixer in 0.000284667 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for SingleQubitCompare from 3 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for And† in 2.1e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for SingleQubitCompare in 0.000618708 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for BiQubitsMixer from 3 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for BiQubitsMixer in 0.000235625 s
INFO:qualtran.resource_counting._costing:Computed gate counts for LessThanEqual in 0.00379679 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for CSwap from 1 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for TwoBitCSwap in 9.54201e-06 s
INFO:qualtran.resource_counting._costing:Computed gate counts for CSwap in 0.000392709 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for QROM from 4 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for And in 1.7333e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for QROM in 0.000783209 s
INFO:qualtran.resource_counting._costing:Computed gate counts for Adjoint(subbloq=StatePreparationAliasSampling) in 0.00864633 s
INFO:qualtran.resource_counting._costing:Computed gate counts for Partition in 2.4958e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for Partition in 1.3958e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for Adjoint(subbloq=AutoPartition) in 0.011195 s
INFO:qualtran.resource_counting._costing:Computed gate counts for Adjoint(subbloq=BlackBoxPrepare) in 0.011832 s
INFO:qualtran.resource_counting._costing:Computed gate counts for Partition in 1.5834e-05 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for BlackBoxSelect from 1 callee(s)
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for AutoPartition from 5 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for Partition in 2.15e-05 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for ApplyLthBloq from 9 callee(s)
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for C[AutoPartition] from 7 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for Partition in 1.8625e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for Partition in 3.0625e-05 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for C[ChebyshevPolynomial] from 3 callee(s)
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for ReflectionUsingPrepare from 4 callee(s)
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for PrepareIdentity from 1 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for I in 3.1125e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for PrepareIdentity in 0.000515125 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for MultiControlZ from 1 callee(s)
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for C[ZGate] from 2 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for CZ in 2.5875e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for C[ZGate] in 0.000989208 s
INFO:qualtran.resource_counting._costing:Computed gate counts for MultiControlZ in 0.00152679 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for Adjoint(subbloq=PrepareIdentity) from 1 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for Adjoint(subbloq=PrepareIdentity) in 0.000336666 s
INFO:qualtran.resource_counting._costing:Computed gate counts for Z in 0.00020575 s
INFO:qualtran.resource_counting._costing:Computed gate counts for ReflectionUsingPrepare in 0.00415179 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for C[Adjoint(subbloq=LinearCombination)] from 9 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for Partition in 3.0916e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for Partition in 1.5333e-05 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for C[Adjoint(subbloq=BlackBoxSelect)] from 1 callee(s)
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for C[Adjoint(subbloq=AutoPartition)] from 5 callee(s)
INFO:qualtran.resource_counting._costing:Computed gate counts for Partition in 3.2459e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for Partition in 1.6042e-05 s
INFO:qualtran.resource_counting._costing:Computed gate counts for Partition in 1.4e-05 s
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for C[Adjoint(subbloq=ApplyLthBloq)] from 6 callee(s)
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for C[C[Adjoint(subbloq=AutoPartition)]] from 3 callee(s)
INFO:qualtran.resource_counting._bloq_counts:Computing gate counts for C[C[Adjoint(subbloq=Unitary)]] from 1 callee(s)

@mpharrigan
Copy link
Collaborator Author

mpharrigan commented Aug 27, 2024

product_block_encoding_symb doesn't work because there is no symbolic decomposition and there is no build_call_graph. @charlesyuan314 can we support symbolic gate counts for product?

✅ done by @charlesyuan314 in #1352

@mpharrigan
Copy link
Collaborator Author

mpharrigan commented Aug 27, 2024

CAdd has build_call_graph and _t_complexity_ that disagree on clifford count (I think). @NoureldinYosri can you investigate and remove the t_complexity method

🚧 #1359 just removes the _t_complexity_ method for expediency. Would still appreciate a second opinion on this

@mpharrigan
Copy link
Collaborator Author

mpharrigan commented Aug 27, 2024

Other bloq examples with mismatched clifford counts

  • cmodadd_example
  • cmodadd_symbolic
  • cmodsub_symb

that I suspect go via CAdd

Last clifford discrepancy:

  • qubitization_qpe_chem_thc

that I haven't investigated yet

@NoureldinYosri
Copy link
Contributor

CAdd has build_call_graph and _t_complexity_ that disagree on clifford count (I think). @NoureldinYosri can you investigate and remove the t_complexity method

it's okay to remove it ... it was written for an old decomposition and had to be fixed before.

@mpharrigan
Copy link
Collaborator Author

These are all fixed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants