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

BooleanHamiltonianGate implementation #4705

Merged
merged 15 commits into from
Feb 28, 2022
Merged

Conversation

daxfohl
Copy link
Collaborator

@daxfohl daxfohl commented Nov 23, 2021

Implementation for BooleanHamiltonianGate.

As implemented, BooleanHamiltonianGate.on(qubits) returns a GateOperation. And BooleanHamiltonianOperation has not been changed to contain any gate. In other words, BooleanHamiltonianGate is completely unrelated to BooleanHamiltonianOperation. They can be considered two different ways to do the same thing. This means we can simply deprecate BooleanHamiltonianOperation and have users replace it with BooleanHamiltonianGate.

The code for BooleanHamiltonianGate is basically identical with BooleanHamiltonianOperation, except instead of Dict[str, Qid]], a mapping is a Sequence[Tuple[str, int]], where the int represents the dimension. It implements the qid_shape protocol from this data, and the GateOperation constructor already has logic to ensure the appropriate number/dimension of qubits are applied.

Tests are added to all effects.

cc @tanujkhattar @tonybruguier

@google-cla google-cla bot added the cla: yes Makes googlebot stop complaining. label Nov 23, 2021
@CirqBot CirqBot added the size: M 50< lines changed <250 label Nov 23, 2021
Copy link
Contributor

@tonybruguier tonybruguier left a comment

Choose a reason for hiding this comment

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

LGTM with a few nits. I am certainly not a Cirq expert, so I will leave it to them to decide on the wisdom of the approach. Thanks for taking this on.

cirq-core/cirq/ops/boolean_hamiltonian.py Show resolved Hide resolved
cirq-core/cirq/ops/boolean_hamiltonian_test.py Outdated Show resolved Hide resolved
cirq-core/cirq/ops/boolean_hamiltonian_test.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@tanujkhattar tanujkhattar left a comment

Choose a reason for hiding this comment

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

Should the BooleanHamiltonianGate be a CircuitOperation instead? Follows from the proposal in #4338 to replace composite gates with CircuitOperations.

@daxfohl
Copy link
Collaborator Author

daxfohl commented Nov 24, 2021

Should the BooleanHamiltonianGate be a CircuitOperation instead? Follows from the proposal in #4338 to replace composite gates with CircuitOperations.

@tanujkhattar Hmm, possibly? I think BooleanHamiltonian would be a denser serialization though, if that's a concern. @95-martin-orion should weigh in on that.

@95-martin-orion
Copy link
Collaborator

I agree with @daxfohl on this one - the gate is preferable. I've gone back and forth a bit on this (as is evident from my comment history), but the point about denser serialization / easier comparison of composite gates vs. CircuitOperations is a good one.

@MichaelBroughton
Copy link
Collaborator

@tanujkhattar These kinda of "more compound style" gates worry me a little bit when it comes to playing nice with gatesets. Do you think there is a nice way to fit this gate and the one proposed in #4696 ? I'm less worried about the GlobalPhaseGate

@daxfohl
Copy link
Collaborator Author

daxfohl commented Nov 30, 2021

@MichaelBroughton Do you mean, remove both HamiltonianBooleanOperation and HamiltonianBooleanGate, and replace those with a module-level function create_boolean_hamiltonian_subcircuit(...) -> CircuitOperation?

I can see how that makes sense, since the benefit of compact serialization is only useful if the target device understands it. Currently nothing else understands what a BooleanHamiltonian is and would just barf.

The downside is it no longer looks as nice in diagrams, just a plain subcircuit. Also a little less flexible in terms of circuit.map_operations--since BH is not a thing in itself, you can't easily map all BH gates with one BH expression to a different BH expression for instance.

I still feel like having the gate is useful for things like the latter. You don't get that flexibility if it's just an anonymous subcircuit.

@tonybruguier
Copy link
Contributor

For a little bit of context, in my original attempt:
914f3d5

I did have a standalone function rather than a dedicated gate. I am OK with going back to gate and I am just giving this information for historical context. Maybe Balint's reasons for going to a gate are important.

If we do decide to go back to a function, I am OK with either Dax doing it, or I can take it up myself (in which case, please let me know where the code should reside).

@daxfohl
Copy link
Collaborator Author

daxfohl commented Dec 1, 2021

I'm obviously biased, but I'm 99% in favor of keeping the approach in this PR, and simply changing this from an operation to a gate. Gates are more flexible and designed for plug and play, hence the original issue to do this #4683. My second choice would be to change it to a factory method that creates a CircuitOperation, like Tony's original attempt, and remove BooleanHamiltonian as a class entirely.

I'm not in favor of having this "be" a CircuitOperation via inheritance. CircuitOperation just brings a lot of cruft to sit on top of. Like it's odd that BooleanHamiltonian.with_measurement_key_mapping would exist, etc. I can see the perspective that "it's just like the factory method but better because now you can still do isinstance(BooleanHamiltonian)", but something still feels off to me with this approach. (If we really want a CircuitOperation that you can determine was a BoolHam, I'd offer that tags might be a better way to do this. But like I said, I think we should just use a gate)

I also don't think we should leave it as-is. We've decided to move ops to gates where possible, and this is a pretty easy one, so we should do something here.

@daxfohl daxfohl marked this pull request as ready for review December 21, 2021 18:27
@daxfohl daxfohl requested review from cduck, vtomole and a team as code owners December 21, 2021 18:27
@daxfohl daxfohl requested a review from viathor December 21, 2021 18:27
@daxfohl
Copy link
Collaborator Author

daxfohl commented Jan 21, 2022

@tanujkhattar Just checking in whether you're waiting on something from my side. (No rush, I can see you're busy with other things, just wanted to make sure I'm not missing something.)

Copy link
Collaborator

@tanujkhattar tanujkhattar left a comment

Choose a reason for hiding this comment

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

LGTM % nits.

cirq-core/cirq/ops/boolean_hamiltonian.py Outdated Show resolved Hide resolved
cirq-core/cirq/ops/boolean_hamiltonian.py Outdated Show resolved Hide resolved
cirq-core/cirq/json_resolver_cache.py Outdated Show resolved Hide resolved
@tanujkhattar tanujkhattar merged commit d2ae1e4 into quantumlib:master Feb 28, 2022
@daxfohl daxfohl deleted the gates-bh branch February 28, 2022 23:11
95-martin-orion pushed a commit to 95-martin-orion/Cirq that referenced this pull request Mar 2, 2022
* BooleanHamiltonianGate implementation

* Replace dict with sequence for order preservation

* Remove logic for qudits, add check that only qubits are accepted.

* Deprecate boolham op
rht pushed a commit to rht/Cirq that referenced this pull request May 1, 2023
* BooleanHamiltonianGate implementation

* Replace dict with sequence for order preservation

* Remove logic for qudits, add check that only qubits are accepted.

* Deprecate boolham op
harry-phasecraft pushed a commit to PhaseCraft/Cirq that referenced this pull request Oct 31, 2024
* BooleanHamiltonianGate implementation

* Replace dict with sequence for order preservation

* Remove logic for qudits, add check that only qubits are accepted.

* Deprecate boolham op
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cla: yes Makes googlebot stop complaining. size: M 50< lines changed <250
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants