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

Native pragma support #1282

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Native pragma support #1282

wants to merge 1 commit into from

Conversation

notmgsk
Copy link
Contributor

@notmgsk notmgsk commented Dec 7, 2020

Description

Add native pragma support to pyQuil.

PyQuil currently allows one to either use Pragma or bare strings to create pragmas. Both leave a lot to be desired:

p = Program('PRAGMA INITIAL_REWIRING "NAIVE"',
            H(0),
            "PRAGMA PRESERVE_BLOCK",
            CNOT(0, 1),
            "PRAGMA END_PRESERVE_BLOCK",
)

p = Program(Pragma("INITIAL_REWIRING", ['"NAIVE"']), # yes really
            H(0),
            Pragma("PRESERVE_BLOCK"),
            CNOT(0, 1),
            Pragma("END_PRESERVE_BLOCK"),
)

They're clumsy and not self-documenting. By adding some extra sugar, we can make pragmas both easier to discover and use:

p = Program(pragmas.NAIVE_REWIRING,
            H(0),
            pragmas.PreserveBlock(CNOT(0, 1)),
)

Closes #661

  • The above description motivates these changes.
  • There is a unit test that covers these changes.
  • All new and existing tests pass locally and on Travis CI.
  • Parameters and return values have type hints with PEP 484 syntax.
  • Functions and classes have useful Sphinx-style docstrings.
  • All code follows Black style and obeys flake8 conventions.
  • (New Feature) The docs have been updated accordingly.
  • (Bugfix) The associated issue is referenced above using auto-close keywords.
  • The changelog is updated, including author and PR number (@username, gh-xxx).

@notmgsk notmgsk marked this pull request as ready for review December 7, 2020 18:30
@notmgsk notmgsk requested a review from a team as a code owner December 7, 2020 18:30
@notmgsk notmgsk mentioned this pull request Dec 10, 2020
return f'PRAGMA INITIAL_REWIRING "{self.rewiring}"'


NAIVE_REWIRING = InitialRewiring("NAIVE")
Copy link
Contributor

Choose a reason for hiding this comment

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

A thought - what about making this an enum to group them together?

class InitialRewiring(Enum):
    Naive = Pragma("PRAGMA INITIAL_REWIRING \"NAIVE\"")
    ....

my_program = Program(
    pragmas.InitialRewiring.Naive,
    ...
)

Then the enum also becomes documentation of what the available options are.

PARTIAL_REWIRING = InitialRewiring("PARTIAL")


class PreserveBlock(Pragma):
Copy link
Contributor

Choose a reason for hiding this comment

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

Could this get tricky? Since it wraps the instructions, it might introduce cases where code that assumed program.instructions would be the whole program would break when it's only a single-element list, and in each place it will have to account for this.

Might be simpler to just vend the pragmas for block start and end separately?

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.

PRAGMA syntax for rewiring is clunky
2 participants