Skip to content

Commit

Permalink
mirror_qv.py
Browse files Browse the repository at this point in the history
  • Loading branch information
WrathfulSpatula committed Oct 25, 2024
1 parent 74994fb commit 96f5c5f
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
File renamed without changes.
75 changes: 75 additions & 0 deletions mirror_qv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# Demonstrate mirror circuit simplification

import math
import random
import sys
import time

from pyqrack import QrackSimulator, QrackCircuit


def bench_qrack(n):
circ = QrackCircuit()

lcv_range = range(n)
all_bits = list(lcv_range)
x_op = [0, 1, 1, 0]
gateSequence = [ 0, 3, 2, 1, 2, 1, 0, 3 ]
row_len = math.ceil(math.sqrt(n))

for _ in lcv_range:
# Single-qubit gates
for i in lcv_range:
th = random.uniform(0, 2 * math.pi)
ph = random.uniform(0, 2 * math.pi)
lm = random.uniform(0, 2 * math.pi)
cos0 = math.cos(th / 2);
sin0 = math.sin(th / 2);
u_op = [
cos0 + 0j, sin0 * (-math.cos(lm) + -math.sin(lm) * 1j),
sin0 * (math.cos(ph) + math.sin(ph) * 1j), cos0 * (math.cos(ph + lm) + math.sin(ph + lm) * 1j)
]
circ.mtrx(u_op, i)

# 2-qubit couplers
unused_bits = all_bits.copy()
random.shuffle(unused_bits)
while len(unused_bits) > 1:
circ.ucmtrx([unused_bits.pop()], x_op, unused_bits.pop(), 1)

# Dig into the (open source) code for yourself:
# Qrack does NOT have a special-case optimization
# when appending specifically the circuit inverse;
# it just simplifies to identity, gate-by-gate.
# (This is not necessarily true for every possible
# "mirror circuit," i.e. any circuit that
# simplifies to identity operator.)
start = time.perf_counter()
sim = QrackSimulator(n)
circ.run(sim)
circ.inverse().run(sim)
if sim.m_all() != 0:
raise Exception("Mirror circuit failed!")
seconds = time.perf_counter() - start
fidelity = sim.get_unitary_fidelity()

return (seconds, fidelity)


def main():
n = 50
if len(sys.argv) > 1:
n = int(sys.argv[1])

results = bench_qrack(n)

print(n, "qubits,",
results[0], "seconds,",
results[1], "fidelity"
)

return 0


if __name__ == '__main__':
sys.exit(main())

0 comments on commit 96f5c5f

Please sign in to comment.