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

Add a classical simulator to Cirq #5822

Closed
tanujkhattar opened this issue Aug 10, 2022 · 17 comments
Closed

Add a classical simulator to Cirq #5822

tanujkhattar opened this issue Aug 10, 2022 · 17 comments
Assignees
Labels
area/simulation kind/feature-request Describes new functionality triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add

Comments

@tanujkhattar
Copy link
Collaborator

tanujkhattar commented Aug 10, 2022

The feature request is to add a classical simulator which can simulate X / CNOT and multi-controlled X gates, similar to ToffoliSimulator in Q#

See https://docs.microsoft.com/en-us/azure/quantum/machines/toffoli-simulator

cc @mpharrigan to add more context

P2 - we should do it in the next couple of quarters

cc @augustehirth This would need documentation once we get this done.

@tanujkhattar tanujkhattar added kind/feature-request Describes new functionality triage/discuss Needs decision / discussion, bring these up during Cirq Cynque labels Aug 10, 2022
@vtomole
Copy link
Collaborator

vtomole commented Aug 10, 2022

xref: #3079

@vtomole vtomole added triage/duplicate and removed triage/discuss Needs decision / discussion, bring these up during Cirq Cynque triage/duplicate labels Aug 10, 2022
@daxfohl
Copy link
Contributor

daxfohl commented Aug 10, 2022

Note we've got a very basic implementation already in the test case for custom_simulator: https://github.com/quantumlib/Cirq/blob/master/cirq-core/cirq/contrib/custom_simulators/custom_state_simulator_test.py

I recommend starting there, copying it to its own non-test file, and then fleshing out the _act_on_fallback_ with @cduck's code https://github.com/cduck/cirqtools/blob/master/cirqtools/classical_simulator.py#L118-L149 linked in #3079.

@dstrain115
Copy link
Collaborator

I have a student who can do this project.

@dstrain115 dstrain115 self-assigned this Aug 11, 2022
@mpharrigan
Copy link
Collaborator

@daxfohl the example custom simulator doesn't support things like e.g. CNOT. Do we have to switch on the gate type in _act_on_fallback_? It's probably necessary for gates themselves to declare how to operate on computational basis states like how gates know how to act on a state vector

@daxfohl
Copy link
Contributor

daxfohl commented Aug 16, 2022

Correct, @cduck's implementation has the proper state transition logic. My example shows how to integrate into the custom simulator infrastructure so that you get subcircuit traversal, classical control, etc. for free. So the actual implementation needs to combine these two things.

@mpharrigan
Copy link
Collaborator

mpharrigan commented Aug 16, 2022

How though? Is _act_on_fallback_ supposed to interrogate the gate to see if it has a magic method that knows how to act on a classical state?

@daxfohl
Copy link
Contributor

daxfohl commented Aug 17, 2022

@mpharrigan
Copy link
Collaborator

I was imagining having methods on classical gates that could express their classical action on a bitstring without tensoring up a unitary

@daxfohl
Copy link
Contributor

daxfohl commented Aug 17, 2022

I see, yeah you could make a new protocol like apply_to_bitstring(bitstring) -> bitstring and supplement cduck's method like

        if cirq.has_application_to_bitstring(op):
           state = cirq.apply_to_bitstring(op, state, indices)
        elif cirq.has_unitary(op):
            self._simulate_unitary(op, state, indices)
        else:
            self._simulate_mixture(op, state, indices)

or something along those lines. That way you get the apply_to_bitstring shortcut plus the ability to fall back to unitary if that protocol was not implemented on the gate.

@dstrain115 dstrain115 added the triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add label Aug 17, 2022
@viathor viathor added triage/discuss Needs decision / discussion, bring these up during Cirq Cynque area/simulation labels Aug 30, 2022
@verult
Copy link
Collaborator

verult commented Aug 31, 2022

From Cirq sync: reverting back to accepted. @viathor let us know if there's anything you'd like to discuss

@verult verult removed the triage/discuss Needs decision / discussion, bring these up during Cirq Cynque label Aug 31, 2022
@viathor
Copy link
Collaborator

viathor commented Sep 1, 2022

Oh, I think I added triage/discuss by accident (probably missed that the issue already had a triage/* label).

@tanujkhattar
Copy link
Collaborator Author

@mpharrigan As discussed offline, can you write a brief comment / code piece describing how the simulator using the protocol to act on a numpy array and falling back to the unitary application would look like? And potential advantages and challenges in using the protocol to act on 2D numpy arrays for sampling multiple classical input states in a vectorized fashion?

@dstrain115 How's the progress on this with the student? Are there any roadblocks in the initial exploration?

@navaro1
Copy link
Contributor

navaro1 commented May 24, 2023

Hello, I'd like to take a stab at this issue. I was not involved before. Will read through current implementation and comment history and then provide a comment with my understanding and strategy for the implementation.

@tanujkhattar tanujkhattar assigned navaro1 and unassigned dstrain115 May 24, 2023
@tanujkhattar
Copy link
Collaborator Author

@navaro1 Thanks! I've assigned the issue to you. Looking forward to your follow up summary comment.

@navaro1
Copy link
Contributor

navaro1 commented May 25, 2023

Here are some key points from the discussion and potential strategy:

  1. A basic implementation already exists in the test case for custom_simulator, and it's recommended to start from there.
  2. Implement _act_on_fallback_ similar to the code in @cduck classical_simulator
  3. A new protocol like apply_to_bitstring(bitstring) -> bitstring could be created, which then could be leveraged to improve the implementation.

Questions (cc: @tanujkhattar):

  1. Should the simulator code be put in cirq-core/cirq/sim or in the other directory?

@navaro1
Copy link
Contributor

navaro1 commented Jun 14, 2023

I can see that there is PR for classical simulator already - #6124. I think it would make sense to reassign this issue to author of that PR for now. I am still interested in helping, but do not want to duplicate the work.

@dstrain115
Copy link
Collaborator

This has been completed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area/simulation kind/feature-request Describes new functionality triage/accepted there is consensus amongst maintainers that this is a real bug or a reasonable feature to add
Projects
No open projects
Status: Done
Development

No branches or pull requests

8 participants