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

Raise TypeError when .run() is called with non-compiled Program #799

Merged
merged 3 commits into from
Feb 11, 2019

Conversation

notmgsk
Copy link
Contributor

@notmgsk notmgsk commented Feb 6, 2019

Couple things.

  1. The test is currently performed in QuantumComputer.run(). Should
    the test be performed in QPU.run()? I chose QC.run() because that is
    where the executable is provided and will show up in the
    backtrace.
  2. That test might not be the best it could be. Since we're not
    running on a QPU, we can't instantiate a true QPU object -- is the
    way I've mocked it appropriate and safe?

Closes #740.

Have a good day :}

@notmgsk notmgsk requested review from mpharrigan, lcapelluto and ecpeterson and removed request for mpharrigan and lcapelluto February 6, 2019 21:51
Couple things.
  1. The test is currently performed in QuantumComputer.run(). Should
  the test be performed in QPU.run()? I chose QC.run() because that is
  where the `executable` is provided and will show up in the
  backtrace.
  2. That test might not be the best it could be. Since we're not
  running on a QPU, we can't instantiate a true `QPU` object -- is the
  way I've mocked it appropriate and safe?

Have a good day :}
@notmgsk notmgsk force-pushed the fix/better-error-for-unexpected-program branch from 89832d3 to 581a41c Compare February 6, 2019 22:16
@@ -144,6 +144,13 @@ def run(self, executable: Executable,
are a list of floats or integers.
:return: A numpy array of shape (trials, len(ro-register)) that contains 0s and 1s.
"""
# This prevents a common error where users expect QVM.run()
# and QPU.run() to be interchangeable. QPU.run() needs the
# supplied executable to have been compiled, QVM.run() does not.
Copy link
Contributor

Choose a reason for hiding this comment

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

some more color here: if you construct a qvm with a topology, the code will assume you care about realism and will error if you pass a program instead of an executable. However, we thought that would be annoying if you didn't really care about compiling/realism so if you construct a {n}q-qvm without a topology it will accept Programs

>> get_qc('2q-qvm').run(program) # fine
>> get_qc('Aspen-1-2Q-A-qvm').run(program) # error! you need to compile

this might be more confusing than it's worth

# This prevents a common error where users expect QVM.run()
# and QPU.run() to be interchangeable. QPU.run() needs the
# supplied executable to have been compiled, QVM.run() does not.
if isinstance(self.qam, QPU) and isinstance(executable, Program):
Copy link
Contributor

Choose a reason for hiding this comment

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

why not put this check in the QPU class?

Copy link
Contributor Author

@notmgsk notmgsk Feb 7, 2019

Choose a reason for hiding this comment

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

From above:

  1. The test is currently performed in QuantumComputer.run(). Should the test be performed in QPU.run()? I chose QC.run() because that is where the executable is provided and will show up in the backtrace.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think if you put it in QPU it will still show up in the backtrace, just one level up

# This test might need some more knowledgeable eyes. Not sure how
# to best mock a qpu.
qc = get_qc('1q-qvm')
qc.qam = QPU(endpoint='tcp://not-needed:00000',
Copy link
Contributor

Choose a reason for hiding this comment

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

I think the legit way to mock this would be to mock out QPU.client.call which is the rpcq thing that actually communicates with the quantum hardware. Maybe the rpcq repo has some useful test fixtures we could repurpose here?

Copy link
Contributor

@lcapelluto lcapelluto left a comment

Choose a reason for hiding this comment

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

A couple minor comments you might want to address. Nice change :)

@@ -5,7 +5,7 @@
import pytest

from pyquil import Program, get_qc, list_quantum_computers
from pyquil.api import QVM, QuantumComputer, local_qvm
from pyquil.api import QVM, QPU, QuantumComputer, local_qvm
Copy link
Contributor

Choose a reason for hiding this comment

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

it doesn't look like this is used anywhere

@@ -120,6 +121,13 @@ def run(self):

:return: The QPU object itself.
"""
# This prevents a common error where users expect QVM.run()
# and QPU.run() to be interchangeable. QPU.run() needs the
# supplied executable to have been compiled, QVM.run() does not.
Copy link
Contributor

Choose a reason for hiding this comment

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

This is good, I don't remember what the error would be otherwise, but this error message is certainly better.

Ideally, QPU and QVM should be interchangeable.
I think a warning is raised when running on QVM without calling .compile, or maybe only in some cases. Maybe we can consider rewording this comment to encourage users to call .compile even when targeting the QVM, to prevent people from running into this issue in the first place.

Copy link
Contributor

Choose a reason for hiding this comment

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

the full story here: #799 (comment)

@@ -5,7 +5,7 @@
import pytest

from pyquil import Program, get_qc, list_quantum_computers
from pyquil.api import QVM, QuantumComputer, local_qvm
from pyquil.api import QVM, QPU, QuantumComputer, local_qvm
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
from pyquil.api import QVM, QPU, QuantumComputer, local_qvm
from pyquil.api import QVM, QuantumComputer, local_qvm

@mpharrigan mpharrigan merged commit 61d4d6e into master Feb 11, 2019
@mpharrigan mpharrigan deleted the fix/better-error-for-unexpected-program branch February 11, 2019 18:54
@karalekas karalekas added this to the v2.4.0 milestone Feb 14, 2019
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.

4 participants