tequilahub / tequila

A High-Level Abstraction Framework for Quantum Algorithms
MIT License
362 stars 101 forks source link

`google` module of `cirq` has been moved to a separate module #307

Closed dariavh closed 12 months ago

dariavh commented 12 months ago

Describe the bug When trying to use the sycamore device for simulating my wavefunction, I get the error AttributeError: module 'cirq' has no attribute 'google' because the cirq.google module has been moved into its own module cirq_google.

To Reproduce Steps to reproduce the behavior:

import tequila as tq

H = tq.paulis.X(0)

wfn = tq.simulate(H, samples=100, device="sycamore", backend="cirq")

Computer (please complete the following information):

Additional context I think the bugfix would consist of renaming all cirq.google references to cirq_google (need to check thoroughly) and adding the cirq_google package to the optional backends list in the requirements.txt. I can provide this in a new PR if you want?

kottmanj commented 12 months ago

That would be amazing!

What would be great to add a small test alongside (in tests/test_simulator_backends.py). E.g.

import importlib
HAS_GOOGLE = importlib.util.find_spec('cirq_google')
@pytest.mark.skipif(condition=HAS_GOOGLE, reason="cirq_google not installed")
def test_cirq_google_devices():
    some tests

and in the same way, including the HAS_GOOGLE in src/tequila/simulators/simulator_cirq.py in order to throw a meaningful error message if someone tries to use the devides that need the cirq_google package installed.

dariavh commented 12 months ago

Hi Jakob,

It seems that a lot has changed in the cirq package over the last years. The Foxtail and Bristlecone devices are deprecated, see 0.14.0 release for the full list. The optimized_for_sycamore() method is deprecated as well and is replaced by optimize_for_target_gateset() that can e.g. optimize an abstract circuit in cirq to gates that are compatible on the Google Sycamore devices.

I am currently working out the details, but I would refactor the build_device_circuit method in simulator_cirq.py as:

if isinstance(device, cirq.Device):
       if device in [cirq_google.Sycamore, cirq_google.Sycamore23]:
             try:
                  circuit = cirq.optimize_for_target_gateset(circuit=c, gateset=cirq_google.SycamoreTargetGateset())
             except ValueError:
                  raise TequilaCirqException('could not optimize for device={}'.format(device))
       else:
             ### under construction (potentially on other branches)
             raise TequilaException('Only Sycamore and Sycamore23 devices currently functional. Sorry!')
kottmanj commented 12 months ago

Looks good to me!

would suggest the following error handling, then the original Exception from cirq is also displayed (often useful)

             except ValueError as E:
                  original_message = str(E)
                  raise TequilaCirqException('original message:\n{}\n\ncould not optimize for device={}'.format(original_message,device))