From 58e2ab61df35578320f4abe912cb794739407cfa Mon Sep 17 00:00:00 2001 From: Mark Skilbeck Date: Mon, 7 Dec 2020 16:07:24 +0000 Subject: [PATCH] native pragma support --- pyquil/pragmas.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 pyquil/pragmas.py diff --git a/pyquil/pragmas.py b/pyquil/pragmas.py new file mode 100644 index 000000000..5effcd8be --- /dev/null +++ b/pyquil/pragmas.py @@ -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") +RANDOM_REWIRING = InitialRewiring("RANDOM") +GREEDY_REWIRING = InitialRewiring("GREEDY") +PARTIAL_REWIRING = InitialRewiring("PARTIAL") + + +class PreserveBlock(Pragma): + 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"