• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

feihoo87 / waveforms / 7087368175

04 Dec 2023 01:33PM UTC coverage: 43.134% (-0.5%) from 43.641%
7087368175

push

github

feihoo87
fix workflow

7413 of 17186 relevant lines covered (43.13%)

2.58 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

72.22
/waveforms/qlisp/macro.py
1
from collections import defaultdict
6✔
2

3
from numpy import cos, mod, pi
6✔
4

5
from waveforms.math.matricies import U, Unitary2Angles
6✔
6

7
from .base import QLispError, head
6✔
8
from .simulator.simple import seq2mat
6✔
9

10

11
def _lookup(name, env):
6✔
12
    try:
6✔
13
        return env.get(name, name)
6✔
14
    except:
×
15
        return name
×
16

17

18
def lookup(st, env):
6✔
19
    if isinstance(st[1], tuple):
6✔
20
        return (st[0], tuple(_lookup(q, env) for q in st[1]))
6✔
21
    elif isinstance(st[1], (str, int)):
6✔
22
        return (st[0], _lookup(st[1], env))
6✔
23
    else:
24
        return st
×
25

26

27
def define_macro(name, value, env):
6✔
28
    env[name] = value
6✔
29

30

31
def call_macro(gate, st):
6✔
32
    qubits = st[1]
6✔
33
    if isinstance(st[0], str):
6✔
34
        args = ()
6✔
35
    else:
36
        args = st[0][1:]
6✔
37
    try:
6✔
38
        yield from gate(qubits, *args)
6✔
39
    except:
×
40
        raise QLispError(f'extend macro {st} error.')
×
41

42

43
def extend_control_gate(st, scope):
6✔
44
    # TODO
45
    gate, qubits = st
×
46
    _, unitary = gate
×
47
    if isinstance(unitary, str):
×
48
        if unitary == 'Z':
×
49
            return [('CZ', qubits)]
×
50
        elif unitary == 'X':
×
51
            return [('Cnot', qubits)]
×
52

53
    # https://doi.org/10.1103/PhysRevA.52.3457
54

55
    unitary = [(unitary, 0)]
×
56
    c, t = qubits
×
57

58
    W = seq2mat(unitary)
×
59
    theta, phi, lam, delta = Unitary2Angles(W)
×
60

61
    if abs(cos(phi + lam) + 1) < 1e-15:
×
62
        return [
×
63
            (('u3', pi / 2 - theta / 2, 0, -phi), t),
64
            ('Cnot', c, t),
65
            (('u3', theta / 2 - pi / 2, phi, 0), t),
66
            (('Rz', delta - pi / 2), t),
67
        ]
68
    else:
69
        return [
×
70
            (('u3', theta / 2, 0, lam), t),
71
            ('Cnot', c, t),
72
            (('u3', -theta / 2, -(phi + lam) / 2, 0), t),
73
            ('Cnot', c, t),
74
            (('u3', 0, 0, (phi - lam) / 2), t),
75
            (('Rz', delta), c),
76
        ]
77

78

79
def extend_macro(qlisp, lib, env=None):
6✔
80
    if env is None:
6✔
81
        env = {}
6✔
82
    for st in qlisp:
6✔
83
        if head(st) == 'define':
6✔
84
            define_macro(st[1], st[2], env)
6✔
85
        elif head(st) == 'C':
6✔
86
            st = lookup(st, env)
×
87
            yield from extend_control_gate(st, lib)
×
88
        else:
89
            st = lookup(st, env)
6✔
90
            gate = lib.getGate(head(st))
6✔
91
            if gate is None:
6✔
92
                yield st
6✔
93
            else:
94
                for st in call_macro(gate, st):
6✔
95
                    yield from extend_macro([st], lib, env)
6✔
96

97

98
_VZ_rules = {}
6✔
99

100

101
def add_VZ_rule(gateName, rule):
6✔
102
    _VZ_rules[gateName] = rule
6✔
103

104

105
def remove_VZ_rule(gateName, rule):
6✔
106
    del _VZ_rules[gateName]
×
107

108

109
def _VZ_P(st, phaseList):
6✔
110
    return [], [mod(phaseList[0] + st[0][1], 2 * pi)]
6✔
111

112

113
def _VZ_rfUnitary(st, phaseList):
6✔
114
    (_, theta, phi, *with_params), qubit = st
6✔
115
    return [(('rfUnitary', theta, phi - phaseList[0], *with_params), qubit)
6✔
116
            ], phaseList
117

118

119
def _VZ_clear(st, phaseList):
6✔
120
    return [st], [0] * len(phaseList)
6✔
121

122

123
def _VZ_exchangable(st, phaseList):
6✔
124
    return [st], phaseList
6✔
125

126

127
def _VZ_swap(st, phaseList):
6✔
128
    return [st], phaseList[::-1]
6✔
129

130

131
add_VZ_rule('P', _VZ_P)
6✔
132
add_VZ_rule('rfUnitary', _VZ_rfUnitary)
6✔
133
add_VZ_rule('Reset', _VZ_clear)
6✔
134
add_VZ_rule('Measure', _VZ_clear)
6✔
135
add_VZ_rule('CZ', _VZ_exchangable)
6✔
136
add_VZ_rule('I', _VZ_exchangable)
6✔
137
add_VZ_rule('Barrier', _VZ_exchangable)
6✔
138
add_VZ_rule('Delay', _VZ_exchangable)
6✔
139
add_VZ_rule('iSWAP', _VZ_swap)
6✔
140
add_VZ_rule('SWAP', _VZ_swap)
6✔
141

142

143
def _exchange_VZ_and_gate(st, phaseList, lib):
6✔
144
    gate = head(st)
6✔
145
    if gate in _VZ_rules:
6✔
146
        return _VZ_rules[gate](st, phaseList)
6✔
147
    else:
148
        raise Exception('Unknow VZ exchange rule.')
×
149

150

151
def reduceVirtualZ(qlisp, lib):
6✔
152
    hold = defaultdict(lambda: 0)
6✔
153

154
    for st in qlisp:
6✔
155
        target = st[1]
6✔
156
        if isinstance(target, (int, str)):
6✔
157
            target = (target, )
6✔
158
        try:
6✔
159
            stList, phaseList = _exchange_VZ_and_gate(
6✔
160
                st, [hold[q] for q in target], lib)
161
            yield from stList
6✔
162
            for q, p in zip(target, phaseList):
6✔
163
                hold[q] = mod(p, 2 * pi)
6✔
164
        except:
×
165
            for q in target:
×
166
                if hold[q] != 0:
×
167
                    yield (('P', hold[q]), q)
×
168
                    hold[q] = 0
×
169
            yield st
×
170

171
    for q in hold:
6✔
172
        if hold[q] != 0:
6✔
173
            yield (('P', hold[q]), q)
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc