请不要使用设置 #2(我让优化运行,这是一个失败的策略)。请进行优化并寻找最合适的方法(并完成 EA)。
我对量子计算这一领域并不太了解,但很久以前就对此感兴趣了。
我向AI提出了一个关于该具体实现方案的问题——并非为了挑刺,而是因为我想在实践中尝试一下——结果收到了详尽且有理有据的回复,其中不仅指出了错误,还提供了修正后的实现方案。 我将对话记录(俄语)和最终版本的源代码(内含多个版本可供选择)附在压缩包中。
double SimulatePureQuantumCircuit(double &features[]) { const int num_states = (int)MathPow(2.0, (double)NUM_QUBITS); // 1. 创建并初始化状态向量(确定性演化) double state[]; ArrayResize(state, num_states); ArrayInitialize(state, 0.0); state[0] = 1.0; // 状态 |000...0> double next_state[]; ArrayResize(next_state, num_states); // --- [演变:Ry旋转阀] --- for(int qubit = 0; qubit < NUM_QUBITS; qubit++) { const int feature_idx = qubit % ArraySize(features); const double angle = MathMax(MathMin(features[feature_idx] * M_PI, M_PI), -M_PI); const double cos_a = MathCos(angle / 2.0); const double sin_a = MathSin(angle / 2.0); ArrayCopy(next_state, state); for(int i = 0; i < num_states; i++) { if((i & (1 << qubit)) == 0) { const int i1 = i | (1 << qubit); next_state[i] = cos_a * state[i] - sin_a * state[i1]; next_state[i1] = sin_a * state[i] + cos_a * state[i1]; } } ArrayCopy(state, next_state); } // --- [进化:CZ的困惑] --- for(int i = 0; i < NUM_QUBITS - 1; i++) { for(int j = i + 1; j < NUM_QUBITS; j++) { const double phase = M_PI / 2.0 * (features[i] * features[j]); if(MathAbs(phase) > M_PI / 4.0) { for(int k = 0; k < num_states; k++) { if(((k & (1 << i)) != 0) && ((k & (1 << j)) != 0)) state[k] = -state[k]; } } } } // --- [真实概率的计算] --- double exact_probs[]; ArrayResize(exact_probs, num_states); for(int i = 0; i < num_states; i++) { // 振幅的平方即为根据波尔定理计算出的纯理论概率 exact_probs[i] = state[i] * state[i]; } // 2. 直接将理想概率传递给投票权重计算 const double prediction = GetWeightedVote(exact_probs); return prediction; }
我没有对新源代码进行任何测试,也没有对AI的论点提出异议。
也许对这一领域更了解的人能给个评价?
附加的文件:
quantum-mql5-safe.zip
841 kb
作为补充,这里提供一个更“综合”的方案,该方案考虑了特征的关联性——这些特征必须成对传递——因此将它们分解为同一量子比特的两个不同部分(实部和虚部)。
// 全局设置(请根据您的交易系统进行调整) #define NUM_QUBITS 3 // 量子比特数(状态向量大小 = 2^NUM_QUBITS) #define MIN_CONFIDENCE 0.1 // 过滤平移信号的最低置信度阈值 //+------------------------------------------------------------------+ //| 快速计算已设置的位(布莱恩·柯尼汉的方法) | //+------------------------------------------------------------------+ int CountBits(int n) { int count = 0; while(n > 0) { n &= (n - 1); count++; } return count; } //+------------------------------------------------------------------+ //| 期望值(Expectation Value)的计算 | //+------------------------------------------------------------------+ double GetWeightedVote(const double &state_probs[]) { double expected_value = 0.0; double total_weight = 0.0; const int size = ArraySize(state_probs); for(int i = 0; i < size; i++) { const double vote_weight = state_probs[i]; if(vote_weight <= 0.000001) continue; const int num_ones = CountBits(i); const double state_value = (2.0 * (double)num_ones / (double)NUM_QUBITS) - 1.0; expected_value += state_value * vote_weight; total_weight += vote_weight; } if(total_weight <= 0.0001) return 0.0; double final_prediction = expected_value / total_weight; if(final_prediction > 1.0) final_prediction = 1.0; if(final_prediction < -1.0) final_prediction = -1.0; return final_prediction; } //+------------------------------------------------------------------+ //| 确定性复量子电路模拟器 | //| features[][0] - 振幅参数,features[][1] - 相位 | //+------------------------------------------------------------------+ double SimulateQuantumCircuitComplex(double &features_matrix[][]) { const int num_states = (int)MathPow(2.0, (double)NUM_QUBITS); const int num_features = ArrayRange(features_matrix, 0); if(num_features == 0) { Print(“错误:特征矩阵为空!”); return 0.0; } // 基于内置类型 complex 的量子态向量 complex state[]; ArrayResize(state, num_states); // 将系统初始化为严格状态 |000...0> for(int i = 0; i < num_states; i++) { state[i].real = 0.0; state[i].imag = 0.0; } state[0].real = 1.0; // 基态振幅为1 // 用于向量原子(同时)更新的临时缓冲区 complex next_state[]; ArrayResize(next_state, num_states); // --- 1. 特征的综合编码(U-Gate:Ry + Rz) --- for(int qubit = 0; qubit < NUM_QUBITS; qubit++) { const int feat_idx = qubit % num_features; // 将输入角度限制在 [-PI; PI] 范围内 const double theta = MathMax(MathMin(features_matrix[feat_idx][0] * M_PI, M_PI), -M_PI); // 适用于 Ry const double phi = MathMax(MathMin(features_matrix[feat_idx][1] * M_PI, M_PI), -M_PI); // 对于 Rz const double cos_t = MathCos(theta / 2.0); const double sin_t = MathSin(theta / 2.0); // 相位移的欧拉指数:e^(i*phi) = cos(phi) + i*sin(phi) complex e_phase; e_phase.real = MathCos(phi); e_phase.imag = MathSin(phi); ArrayCopy(next_state, state); for(int i = 0; i < num_states; i++) { if((i & (1 << qubit)) == 0) { const int i1 = i | (1 << qubit); // 旋转风门的复矩阵 U(theta, phi): // [ cos(theta/2) , -sin(theta/2) ] // [ sin(theta/2)*e^(i*phi), cos(theta/2)*e^(i*phi) ] // 目标位 = 0 的计算 next_state[i].real = cos_t * state[i].real - sin_t * state[i1].real; next_state[i].imag = cos_t * state[i].imag - sin_t * state[i1].imag; // 目标位 = 1 的计算(考虑相位偏移 e^i*phi) complex temp_i1; temp_i1.real = sin_t * state[i].real + cos_t * state[i1].real; temp_i1.imag = sin_t * state[i].imag + cos_t * state[i1].imag; // 通过 MQL5 原生复数乘法实现相位偏移 next_state[i1] = temp_i1 * e_phase; } } ArrayCopy(state, next_state); } // --- 2. 诚实的综合量子纠缠(Controlled-Phase Gate) --- for(int i = 0; i < NUM_QUBITS - 1; i++) { for(int j = i + 1; j < NUM_QUBITS; j++) { const int idx_i = i % num_features; const int idx_j = j % num_features; // 将两个相互关联的参数的复数相位相互混淆 const double cross_phase = M_PI / 2.0 * (features_matrix[idx_i][0] * features_matrix[idx_j][1]); complex cz_gate; cz_gate.real = MathCos(cross_phase); cz_gate.imag = MathSin(cross_phase); for(int k = 0; k < num_states; k++) { // 如果两个量子比特都处于1态,则平滑地旋转它们的总复相位 if(((k & (1 << i)) != 0) && ((k & (1 << j)) != 0)) { state[k] = state[k] * cz_gate; } } } } // --- 3. 精确理论概率的计算(确定性步骤) --- double exact_probs[]; ArrayResize(exact_probs, num_states); for(int i = 0; i < num_states; i++) { // 根据波恩定律,概率 P = |ψ|² = 实部² + 虚部² exact_probs[i] = (state[i].real * state[i].real) + (state[i].imag * state[i].imag); } // --- 4. 获取最终预测 --- const double prediction = GetWeightedVote(exact_probs); const double confidence = MathAbs(prediction); if(confidence < MIN_CONFIDENCE) { return 0.0; // 从量子链的角度来看,市场处于盘整/不明朗状态 } return prediction; }
传递相关参数最方便的方式是通过特征矩阵 features[][],其第二维为 2,其中:
- features[i][0] — 振幅特征(指定实部旋转角(Ry))。
- features[i][1] — 相位特征(指定复数旋转角 (Rz))。
例如,调用代码可以传递以下配对:[价格变动;成交量变动] 或 [RSI 位置;当前 ATR 波动率]。
新文章 从Python到MQL5:量子启发式交易系统的探索之旅已发布:
在这场对量子启发式交易系统的全面探索中,我们将踏上一段旅程,将量子计算的理论概念与现实世界的交易应用紧密相连。本教程从量子计算的基础知识讲起,直至最终实现一个可在实际中应用的MQL5系统,旨在引导您完成整个开发流程。我们将探讨交易如何从量子概念中获益,阐述从Python原型开发到MQL5集成的开发方法,并展示实际性能数据和代码实现。
本文探讨了量子启发式概念在交易系统中的应用,将理论量子计算与MQL5中的实际实现相结合。我们将介绍基本的量子原理,并指导您从Python原型开发过渡到MQL5集成,同时提供实际性能数据。
与传统依赖二元决策的交易不同,量子启发式交易模型利用与量子现象相似的市场行为——多重并发状态、相互关联以及状态的突然转变。通过使用Qiskit等量子模拟器,我们可以在经典计算机上应用量子启发式算法来处理市场的不确定性,并生成有预测性的见解。
作者:Javier Santiago Gaston De Iriarte Cabrera