-
Notifications
You must be signed in to change notification settings - Fork 18
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
Conversation
Codecov Report
@@ 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
Continue to review full report at Codecov.
|
There was a problem hiding this 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
pennylane_cirq/cirq_device.py
Outdated
_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), | ||
} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.") |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
pennylane_cirq/simulator_device.py
Outdated
return self._simulator.simulate_expectation_values( | ||
self.circuit, self.to_paulistring(observable) | ||
)[0] |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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): |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh i see
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
And that's it!