forked from yinfupai/LiuYaoForQuantum
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLiuYao.py
110 lines (88 loc) · 2.68 KB
/
LiuYao.py
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import numpy as np
import matplotlib.pyplot as plt
from qiskit import QuantumCircuit, ClassicalRegister, QuantumRegister, Aer
from qiskit.visualization import plot_state_city
from qiskit import execute, IBMQ
import json
backend = Aer.get_backend('statevector_simulator')
#backend = Aer.get_backend('qasm_simulator')
#归藏出来的为0~7对应乾兑离震巽坎艮坤,对应世爻序列
# gua = GetGua()
# shi = GetShi(gua)
class LiuYao:
def __init__(self):
self.Gua = self.GetGua()
self.ShiYaoPos = self.GetShi(self.Gua)
#计算1的个数
def NumberOfOne(self, n):
if n < 0:
n = n & 0xffffffff
count = 0
while n:
count += 1
n = (n - 1) & n
return count
#获取一个爻位
def GetYao(self):
#记录出现的反面次数
yao_count = 0
#重复3次,得到卦爻变化
for i in range(3):
circ = QuantumCircuit(1)
#转换成叠加态
circ.h(0)
circ.measure_all()
job = execute(circ, backend, shots=1)
result = job.result()
outputstate = result.get_statevector(circ, decimals=0)
#如果为0
if (outputstate[0] == 0):
yao_count = yao_count + 1
#得到一个1时,为少阳,阳变阳
#得到两个1时,为少阴,阴变阳
#得到三个1时,为老阳,阳变阴
#得到零个1时,为老阴,阴变阳
if (yao_count == 0):
return [0, 1]
if (yao_count == 1):
return [1, 1]
if (yao_count == 2):
return [0, 0]
if (yao_count == 3):
return [1, 0]
#获取卦与变卦
def GetGua(self):
gua = [[0, 0, 0, 0, 0, 0], [0, 0, 0, 0, 0, 0, 0]]
for i in range(6):
yao = self.GetYao()
gua[0][i] = yao[0]
gua[1][i] = yao[1]
return gua
#获取世应
def GetShi(self,gua):
sy = [2, 1, 3, 0, 3, 2, 4, 5]
gz = [0, 0, 0]
for i in range(3):
y1 = gua[0][i]
y2 = gua[0][i + 3]
gz[i] = y1 ^ y2
return sy[gz[0] + gz[1] * 2 + gz[2] * 4]
#输出测试结果
if __name__ == "__main__":
ly = LiuYao()
gua = ly.Gua
pos = ly.ShiYaoPos
for i in range(6):
if (gua[0][i] == 0):
print('- -', end='\t')
else:
print('---', end='\t')
if (i == pos):
print('世', end='\t')
else:
print('', end='\t')
if (gua[1][i] == 0):
print('- -', end='\t')
else:
print('---', end='\t')
print('', end='\r\n')