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

Added custom simulator option #51

Merged
merged 9 commits into from
Feb 18, 2021
Merged

Added custom simulator option #51

merged 9 commits into from
Feb 18, 2021

Conversation

chaserileyroberts
Copy link

Allows users to use custom cirq simulators with the existing cirq plugin.

Main reason for this is to support the new Floq integration. To use floq, users simply now can do

import pennylane as qml
import remote_cirq 

floq_sim = remote_cirq.RemoteSimulator("MY_API_KEY")

dev = qml.device("cirq.simulator", wires=2, simulator=floq_sim)

@qml.qnode(dev)
def my_circuit():
    # ....
    return qml.expval(...)

And that's it!

@codecov
Copy link

codecov bot commented Feb 18, 2021

Codecov Report

Merging #51 (d3039f1) into master (4e502d8) will increase coverage by 0.04%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master      #51      +/-   ##
==========================================
+ Coverage   99.07%   99.11%   +0.04%     
==========================================
  Files           8        8              
  Lines         323      338      +15     
==========================================
+ Hits          320      335      +15     
  Misses          3        3              
Impacted Files Coverage Δ
pennylane_cirq/cirq_device.py 98.90% <100.00%> (+0.15%) ⬆️
pennylane_cirq/simulator_device.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4e502d8...10b930b. Read the comment docs.

Copy link
Member

@josh146 josh146 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 from my end! Don't forget to add a changelog entry, just so we can keep track for the next release

Comment on lines 160 to 167
_observable_map = {
"PauliX": None,
"PauliY": None,
"PauliZ": None,
"Hadamard": None,
"PauliX": CirqOperation(lambda: cirq.X),
"PauliY": CirqOperation(lambda: cirq.Y),
"PauliZ": CirqOperation(lambda: cirq.Z),
"Hadamard": CirqOperation(lambda: cirq.H),
"Hermitian": None,
"Identity": None,
"Identity": CirqOperation(lambda: cirq.I),
}
Copy link
Member

Choose a reason for hiding this comment

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

This dictionary is actually useful now

Copy link
Author

Choose a reason for hiding this comment

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

lol


cirq_op = self._observable_map[observable.name]
if cirq_op is None:
raise NotImplementedError(f"{observable.name} is not currently supported.")
Copy link
Member

Choose a reason for hiding this comment

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

I assume this catches qml.Hermitian?

Out of curiosity, are there any observables cirq supports that PL doesn't?

Copy link
Member

Choose a reason for hiding this comment

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

We could use qml.utils.decompose_hamiltonian() to convert the Hermitian matrix to a Pauli string, but not sure if it's worth it

Copy link
Author

Choose a reason for hiding this comment

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

I can add a TODO

Comment on lines 195 to 197
return self._simulator.simulate_expectation_values(
self.circuit, self.to_paulistring(observable)
)[0]
Copy link
Member

Choose a reason for hiding this comment

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

Nice, I suppose this leads to a performance improvement?

Copy link
Author

Choose a reason for hiding this comment

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

Yes, it essentially moves the responsibility of calculating the expectation value to the cirq device rather than just pennylane. The downstream device can then optimize how it's calculated.

Copy link
Member

Choose a reason for hiding this comment

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

Awesome. A lot of PennyLane plugin patterns are repeated from a time (a long way!) back when other frameworks didn't provide (accessible) expectation APIs. Now that this is commonplace, we should take this into account more when writing plugins.

Copy link
Author

Choose a reason for hiding this comment

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

Agreed, especially for more future remote services (both simulation and real QC).

Copy link
Member

Choose a reason for hiding this comment

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

Yep. It also avoids needing to carry around a potentially large probability array longer than we need to.

qml.PauliX(0)
return qml.expval(qml.PauliX(0))

assert circuit() == 0.123
Copy link
Member

Choose a reason for hiding this comment

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

nice!


def __init__(self, wires, shots=1000, analytic=True, qubits=None):
# pylint: disable=too-many-arguments
def __init__(self, wires, shots=1000, analytic=True, qubits=None, simulator=None):
Copy link
Member

Choose a reason for hiding this comment

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

Oh, also don't forget to add the new keyword argument to the docstring

Copy link
Author

Choose a reason for hiding this comment

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

I would but there wasn't a docstring before. I can add one now tho.

Copy link
Member

Choose a reason for hiding this comment

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

There's one above, below the class : line :)

Copy link
Author

Choose a reason for hiding this comment

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

oh i see

@chaserileyroberts chaserileyroberts merged commit 82b1ddd into master Feb 18, 2021
@chaserileyroberts chaserileyroberts deleted the floq_integration branch February 18, 2021 17:37
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.

2 participants