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
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions pyquil/pragmas.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
##############################################################################
# Copyright 2016-2018 Rigetti Computing
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
##############################################################################

from typing import List, Iterable

from pyquil.quilbase import AbstractInstruction, Pragma
from pyquil.quil import Program


class InitialRewiring(Pragma):
def __init__(self, rewiring: str):
self.rewiring = rewiring

def out(self) -> str:
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.

RANDOM_REWIRING = InitialRewiring("RANDOM")
GREEDY_REWIRING = InitialRewiring("GREEDY")
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?

def __init__(self, *instructions: Iterable[AbstractInstruction]):
self.instructions = Program(instructions)

def out(self) -> str:
return "PRAGMA PRESERVE_BLOCK\n{}\nPRAGMA END_PRESERVE_BLOCK".format(
"\n".join([str(instr) for instr in self.instructions]),
)


class CommutingBlocks(Pragma):
def __init__(self, blocks: List[Iterable[AbstractInstruction]]):
self.blocks = blocks

def out(self) -> str:
blocks = "\n".join([f"PRAGMA BLOCK\n{block}PRAGMA END_BLOCK" for block in self.blocks])
return f"PRAGMA COMMUTING_BLOCKS\n{blocks}\nPRAGMA END_COMMUTING_BLOCKS"