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

TypeError when performing gauge optimization with respect to fidelity or trace distance #406

Closed
rileyjmurray opened this issue Mar 1, 2024 · 4 comments
Milestone

Comments

@rileyjmurray
Copy link
Collaborator

rileyjmurray commented Mar 1, 2024

Running gauge optimization with respect to fidelity hits the following codeblock:

elif gates_metric == "fidelity":
for opLbl in mdl.operations:
wt = item_weights.get(opLbl, opWeight)
ret += wt * (1.0 - _tools.entanglement_fidelity(
target_model.operations[opLbl], mdl.operations[opLbl]))**2

The documentation of entanglement_fidelity says its arguments need to be numpy arrays. However, the code above passes in pyGSTi modelmember objects of one kind or another. If you take a quick look at the implementation of entanglement_fidelity then you'll find that this can easily result in an error. (For example, if either operation is a CompsedOp object.)

This specific issue could be fixed by replacing 638 -- 640 in the gaugeopt.py snippet to the following:

op_a = target_model.operations[opLbl].to_dense()
op_b = mdl.operations[opLbl].to_dense()
infidelity = 1.0 - _tools.entanglement_fidelity(op_a, op_b, mxBasis)
ret += wt*(infidelity ** 2)

That said, the issue isn't isolated to the snippet above. Similar bugs exist throughout the definition of the the objective function for non-LS gauge optimization models.

@coreyostrove
Copy link
Contributor

Great job catching this, @rileyjmurray!

I'm not positive what the history is behind this, but I suspect this is vestigial from the time before more complicated modelmembers for operations existed when pretty much all of the operations inherited from DenseOperatorInterface or similar, and as such behaved in many contexts as if they were numpy arrays. This is clearly no longer true, and your proposed solution makes sense to me.

@enielse
Copy link
Collaborator

enielse commented Mar 20, 2024 via email

@sserita
Copy link
Contributor

sserita commented Apr 2, 2024

I think this particular bug is basically solved by Timo's PR #414. By performing the to_dense cast inside entanglement_fidelity, we avoid having to find all the places where the function is called.

The only thing I haven't check as part of that is whether similar bugs exist for other optool functions, which I'll do before closing out this issue.

@sserita sserita added the fixed-but-not-in-release-yet Bug has been fixed, but isn't in an official release yet (just exists on a development branch) label Apr 2, 2024
@sserita
Copy link
Contributor

sserita commented Apr 4, 2024

Closing as Timo's PR did in fact fix the other possible optool functions (average_gate_fidelity and unitarity).

@sserita sserita closed this as completed Apr 4, 2024
@sserita sserita removed the fixed-but-not-in-release-yet Bug has been fixed, but isn't in an official release yet (just exists on a development branch) label Apr 17, 2024
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

No branches or pull requests

4 participants