File size: 2,310 Bytes
d6f21bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
"""Resource-level QIR to Fibonacci braid backend."""

from __future__ import annotations

from dataclasses import dataclass
from typing import Iterable, List

from qlambda.compiler import QIRInstruction


@dataclass(frozen=True)
class BraidOp:
    sigma: int
    forward: bool = True


class TopologicalBraidBackend:
    """Compile QIR gate names to Fibonacci braid words.



    This is a resource-level backend. It emits braid-word schedules for

    accounting and falsification tests; it is not a matrix-equivalence proof.

    """

    H_BRAID = (BraidOp(0), BraidOp(1), BraidOp(0), BraidOp(1), BraidOp(0))
    X_BRAID = (BraidOp(0), BraidOp(0))
    S_BRAID = (BraidOp(0), BraidOp(0))
    CNOT_BRAID = (BraidOp(2), BraidOp(1), BraidOp(0), BraidOp(1), BraidOp(2))
    CCX_BRAID = (
        BraidOp(4), BraidOp(5), BraidOp(4), BraidOp(5), BraidOp(4),
        BraidOp(2), BraidOp(3), BraidOp(4), BraidOp(2), BraidOp(3), BraidOp(4),
        BraidOp(4), BraidOp(5), BraidOp(4), BraidOp(5), BraidOp(4),
    )

    def __init__(self, sk_t_length: int = 300):
        self.sk_t_length = sk_t_length

    def compile(self, qir: Iterable[QIRInstruction]) -> List[BraidOp]:
        braids: List[BraidOp] = []
        for inst in qir:
            braids.extend(self.compile_gate(inst))
        return braids

    def compile_gate(self, inst: QIRInstruction) -> List[BraidOp]:
        gate = inst.gate.upper()
        if gate == "X":
            return list(self.X_BRAID)
        if gate == "H":
            return list(self.H_BRAID)
        if gate == "S":
            return list(self.S_BRAID)
        if gate in {"T", "TDG"}:
            forward = gate == "T"
            return [BraidOp(0, forward=forward) for _ in range(self.sk_t_length)]
        if gate == "CX":
            return list(self.CNOT_BRAID)
        if gate == "CCX":
            return list(self.CCX_BRAID)
        if gate in {"ROTR", "SHR", "BARRIER"}:
            return []
        if gate.endswith("_DAGGER"):
            base = QIRInstruction(gate[:-7], inst.controls, inst.targets, inst.params)
            return [BraidOp(op.sigma, not op.forward) for op in reversed(self.compile_gate(base))]
        raise NotImplementedError(f"Gate {inst.gate!r} has no topological braid mapping")