瀏覽代碼

Add better notation

master
Pete Shadbolt 8 年之前
父節點
當前提交
b41cc7945d
共有 2 個文件被更改,包括 17 次插入2 次删除
  1. +8
    -2
      abp/qi.py
  2. +9
    -0
      tests/test_qi.py

+ 8
- 2
abp/qi.py 查看文件

@@ -8,7 +8,7 @@ And a circuit-model simulator

import numpy as np
import itertools as it
from fractions import Fraction

def hermitian_conjugate(u):
""" Shortcut to the Hermitian conjugate """
@@ -128,5 +128,11 @@ class CircuitModel(object):
for i in range(self.d):
label = bin(i)[2:].rjust(self.nqubits, "0")
if abs(self.state[i, 0]) > 0.00001:
s += "|{}>: {:.2f}\n".format(label, self.state[i, 0])
term = self.state[i, 0]
real_sign = " " if term.real>=0 else "-"
real_frac = Fraction(str(term.real**2)).limit_denominator()
imag_sign = "+" if term.imag>=0 else "-"
imag_frac = Fraction(str(term.imag**2)).limit_denominator()
s += "|{}>: \t{}√{}\t{} i √{}\n".format(
label, real_sign, real_frac, imag_sign, imag_frac)
return s

+ 9
- 0
tests/test_qi.py 查看文件

@@ -1,6 +1,7 @@
import numpy as np
from abp import qi, GraphState
from tqdm import tqdm
import mock

DEPTH = 1000

@@ -161,3 +162,11 @@ def test_against_chp(n=5):
chp.act_cnot(a, b)
psi.act_cnot(a, b)
assert psi == get_chp_state()

def test_sqrt_notation(n=2):
""" Test that SQRT notation looks nice """
c = mock.random_stabilizer_circuit(n)
g = GraphState(range(n))
g.act_circuit(c)
print g.to_state_vector()


Loading…
取消
儲存