conda env list
查一下环境。
conda activate myenv
conda install jupyter
conda install ipykernel
python -m ipykernel install --user --name=myenv --display-name="Python (myenv)"
弄好之后重新打开一下anaconda,再去跑一下代码就好。
import numpy as np
import kaiwu as kw
import pandas as pd
# Define the input adjacency matrix for the graph
adj_matrix = np.array([
[0, 1, 0, 1, 1, 0, 0, 1, 1, 0],
[1, 0, 1, 0, 0, 1, 1, 1, 0, 0],
[0, 1, 0, 1, 1, 0, 0, 0, 1, 0],
[1, 0, 1, 0, 0, 1, 1, 0 ,1, 0],
[1, 0, 1, 0, 0, 1, 0, 1, 0, 1],
[0, 1, 0, 1, 1, 0, 0, 0, 1, 1],
[0, 1, 0, 1, 0, 0, 0, 0, 0, 1],
[1, 1, 0, 0, 1, 0, 0, 0, 1, 0],
[1, 0, 1, 1, 0, 1, 0, 1, 0, 1],
[0, 0, 0, 0, 1, 1, 1, 0, 1, 0]])
# Get the number of nodes in the graph
num_nodes = len(adj_matrix)
# Create a QUBO variable array 'x' with 'num_nodes' variables
# each representing a node in the graph
x = kw.qubo.ndarray(num_nodes,'x',kw.qubo.Binary)
# Define the objective function for the QUBO model of Max cut problem
obj = -x.T @ adj_matrix @ (1-x)
# parse QUBO
obj = kw.qubo.make(obj)
# Extract the QUBO matrix and store it in a pandas DataFrame
qubo_matrix = kw.qubo.qubo_model_to_qubo_matrix(obj)['qubo_matrix']
# Check whether the QUBO matrix satisfy the precision requirement
kw.qubo.check_qubo_matrix_bit_width(qubo_matrix)
# Save the QUBO matrix to a CSV file without including index or header
df = pd.DataFrame(qubo_matrix)
csv_file_path = 'qubo_matrix.csv'
#这个路径你们可以改一下,这样你们就可以在指定路径看到文件了
df.to_csv(csv_file_path,index=False,header=False)
接下来是解释一下授权码:
import kaiwu as kw
kw.license.init(user_id="", sdk_code="")
小结:玻色量子安装起来不难,有两个点需要注意一下:
python --version
第三,注意要在anaconda的new的下拉菜单中确保正常显示这个环境,确保下次打开是有的。