Skip to content

Commit

Permalink
Merge pull request #157 from dlyongemallo/fix_ry
Browse files Browse the repository at this point in the history
Fix direction of qasm `ry` rotation and add `y` gate.
  • Loading branch information
jvdwetering authored Sep 17, 2023
2 parents 70beb84 + 8061f3e commit e4fe332
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 3 deletions.
12 changes: 11 additions & 1 deletion pyzx/circuit/gates.py
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ def __str__(self) -> str:
return 'QRot["exp(-i%Y)",{!s}]({!s})'.format(math.pi*self.phase/2,self.target)

def to_basic_gates(self):
return [ZPhase(self.target, Fraction(1,2)), XPhase(self.target, self.phase), ZPhase(self.target, -Fraction(1,2))]
return [ZPhase(self.target, Fraction(1,2)), XPhase(self.target, -self.phase), ZPhase(self.target, -Fraction(1,2))]

def to_graph(self, g, q_mapper, c_mapper):
for gate in self.to_basic_gates():
Expand All @@ -465,6 +465,15 @@ def to_graph(self, g, q_mapper, c_mapper):
def tcount(self):
return 1 if self.phase.denominator > 2 else 0

class Y(YPhase):
name = 'Y'
qasm_name = 'y'
qc_name = 'Y'
quipper_name = 'Y'
print_phase = False
def __init__(self, target: int) -> None:
super().__init__(target, phase = Fraction(1,1))

class NOT(XPhase):
name = 'NOT'
qasm_name = 'x'
Expand Down Expand Up @@ -990,6 +999,7 @@ def to_graph(self, g, q_mapper, c_mapper):

qasm_gate_table: Dict[str, Type[Gate]] = {
"x": NOT,
"y": Y,
"z": Z,
"s": S,
"t": T,
Expand Down
2 changes: 1 addition & 1 deletion pyzx/circuit/qasmparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ def parse_command(self, c: str, registers: Dict[str,Tuple[int,int]]) -> List[Gat
for g in circ.gates:
gates.append(g.reposition(argset))
continue
if name in ('x', 'z', 's', 't', 'h', 'sdg', 'tdg'):
if name in ('x', 'y', 'z', 's', 't', 'h', 'sdg', 'tdg'):
if name in ('sdg', 'tdg'):
g = qasm_gate_table[name](argset[0],adjoint=True) # type: ignore # mypy can't handle -
else: g = qasm_gate_table[name](argset[0]) # type: ignore # - Gate subclasses with different numbers of parameters
Expand Down
2 changes: 1 addition & 1 deletion tests/test_ry.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ def test_load_qasm_from_file(self):
def test_ry_preserves_graph_semantics(self):
g = self.c.to_graph()
t = tensor_to_matrix(tensorfy(g, False), 1, 1)
expected_t = np.asarray([[np.cos(np.pi/8), np.sin(np.pi/8)], [-np.sin(np.pi/8), np.cos(np.pi/8)]])
expected_t = np.asarray([[np.cos(np.pi/8), -np.sin(np.pi/8)], [np.sin(np.pi/8), np.cos(np.pi/8)]])
self.assertTrue(compare_tensors(t, expected_t, False))

0 comments on commit e4fe332

Please sign in to comment.