33# import pytest
44# import numpy as np
55#
6- # # 添加项目路径到系统路径
76# sys.path.append((Path.cwd().parent.parent).__str__())
87#
98# from pyqpanda3.core import QProg, RX, CPUQVM
109# from pyqpanda_alg.QAOA import default_circuits
1110#
1211#
1312# class TestCompleteXYMixer:
14- # """测试 complete_xy_mixer 接口"""
1513#
1614# def setup_method(self):
17- # """测试前的初始化"""
1815# self.machine = CPUQVM()
1916#
2017# def calculate_hamming_weight_distribution(self, prob_dict, max_weight):
21- # """计算汉明权重分布"""
2218# weight_probs = {}
2319# for weight in range(max_weight + 1):
2420# prob = sum(value for key, value in prob_dict.items()
2723# return weight_probs
2824#
2925# def test_complete_xy_mixer_hamming_weight_preservation(self):
30- # """测试XY mixer应该保持汉明权重"""
3126# n_qubits = 4
3227# prog = QProg(n_qubits)
3328# qubits = prog.qubits()
3429#
35- # # 创建初始状态 - 使用随机RX门创建复杂初始状态
3630# for q in qubits:
3731# prog << RX(q, np.random.random() * 2 * np.pi)
3832#
39- # # 获取初始状态的概率分布
4033# self.machine.run(prog, shots=1)
4134# original_result = self.machine.result().get_prob_dict()
4235#
43- # # 计算初始汉明权重分布
4436# original_weight_probs = self.calculate_hamming_weight_distribution(original_result, n_qubits)
4537#
4638# # 应用 complete_xy_mixer
4739# beta = np.pi / 5
4840# circuit = default_circuits.complete_xy_mixer(qubits, beta)
4941# prog << circuit
5042#
51- # # 获取应用mixer后的概率分布
5243# self.machine.run(prog, shots=1)
5344# final_result = self.machine.result().get_prob_dict()
5445#
55- # # 计算最终汉明权重分布
5646# final_weight_probs = self.calculate_hamming_weight_distribution(final_result, n_qubits)
57- #
58- # # 打印详细的权重分布变化
5947# print("\nHamming weight distribution comparison:")
6048# for weight in range(n_qubits + 1):
6149# original_prob = original_weight_probs.get(weight, 0)
6250# final_prob = final_weight_probs.get(weight, 0)
6351# print(f"Hamming weight {weight}: {original_prob:.4f} -> {final_prob:.4f}")
6452#
65- # # XY mixer应该保持汉明权重(在统计误差范围内)
66- # # 由于有限shots和数值精度,允许一定的误差
67- # tolerance = 0.05 # 5% 容忍度
53+ # tolerance = 0.05
6854# assert abs(original_prob - final_prob) < tolerance, \
6955# f"Hamming weight {weight} not preserved within tolerance: {original_prob:.4f} -> {final_prob:.4f}"
7056#
7157#
7258# if __name__ == "__main__":
73- # # 运行测试
7459# pytest.main([__file__, "-v", "-s"])
0 commit comments