Skip to content

Commit f72a50c

Browse files
committed
native pragma support
1 parent 2ceb0ac commit f72a50c

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

pyquil/pragmas.py

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
##############################################################################
2+
# Copyright 2016-2018 Rigetti Computing
3+
#
4+
# Licensed under the Apache License, Version 2.0 (the "License");
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
7+
#
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
15+
##############################################################################
16+
17+
from typing import List, Iterable
18+
19+
from pyquil.quilbase import AbstractInstruction, Pragma
20+
from pyquil.quil import Program
21+
22+
23+
class InitialRewiring(Pragma):
24+
def __init__(self, rewiring: str):
25+
self.rewiring = rewiring
26+
27+
def out(self) -> str:
28+
return f'PRAGMA INITIAL_REWIRING "{self.rewiring}"'
29+
30+
31+
NAIVE_REWIRING = InitialRewiring("NAIVE")
32+
RANDOM_REWIRING = InitialRewiring("RANDOM")
33+
GREEDY_REWIRING = InitialRewiring("GREEDY")
34+
PARTIAL_REWIRING = InitialRewiring("PARTIAL")
35+
36+
37+
class PreserveBlock(Pragma):
38+
def __init__(self, *instructions: Iterable[AbstractInstruction]):
39+
self.instructions = Program(instructions)
40+
41+
def out(self) -> str:
42+
return "PRAGMA PRESERVE_BLOCK\n{}\nPRAGMA END_PRESERVE_BLOCK".format(
43+
"\n".join([str(instr) for instr in self.instructions]),
44+
)
45+
46+
47+
class CommutingBlocks(Pragma):
48+
def __init__(self, blocks: List[Iterable[AbstractInstruction]]):
49+
self.blocks = blocks
50+
51+
def out(self) -> str:
52+
blocks = "\n".join([f"PRAGMA BLOCK\n{block}PRAGMA END_BLOCK" for block in self.blocks])
53+
return f"PRAGMA COMMUTING_BLOCKS\n{blocks}\nPRAGMA END_COMMUTING_BLOCKS"

0 commit comments

Comments
 (0)