I don’t know much about quantum computing, but I’ve been interested in it for a long time.
I sent the AI a question about this specific implementation – not to nitpick, but because I had an idea to experiment with it in practice – and received a detailed, well-reasoned reply analysing the errors and providing a corrected/improved implementation. I’ve attached the conversation (in Russian) in the archive, along with the final version of the source code (there are several options inside to choose from).
double SimulatePureQuantumCircuit(double &features[]) { const int num_states = (int)MathPow(2.0, (double)NUM_QUBITS); // 1. Create and initialise the state vector (deterministic evolution) double state[]; ArrayResize(state, num_states); ArrayInitialize(state, 0.0); state[0] = 1.0; // State |000...0> double next_state[]; ArrayResize(next_state, num_states); // --- [Evolution: Ry rotary valves] --- 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); } // --- [Evolution: CZ’s Confusion] --- 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]; } } } } // --- [Calculation of true probabilities] --- double exact_probs[]; ArrayResize(exact_probs, num_states); for(int i = 0; i < num_states; i++) { // The square of the amplitude is the pure theoretical probability according to Born’s rule exact_probs[i] = state[i] * state[i]; } // 2. We immediately pass the ideal probabilities on to the vote weighting stage const double prediction = GetWeightedVote(exact_probs); return prediction; }
I haven’t carried out any checks on the new source code, nor have I disputed the AI’s reasoning.
Perhaps someone more knowledgeable on the subject could assess it?
As a supplement, here is a more ‘comprehensive’ variant that takes into account the correlation between features – they must be transmitted in pairs – and therefore splits them into two distinct parts (real and imaginary) of a single qubit.
// Global settings (adjust these to suit your trading system) #define NUM_QUBITS 3 // Number of qubits (size of the state vector = 2^NUM_QUBITS) #define MIN_CONFIDENCE 0.1 // Minimum confidence threshold for filtering the flat // //+------------------------------------------------------------------+ //| Quick count of set bits (Brian Kernighan’s method) | //+------------------------------------------------------------------+ int CountBits(int n) { int count = 0; while(n > 0) { n &= (n - 1); count++; } return count; } //+------------------------------------------------------------------+ //| Calculating the mathematical expectation (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; } //+------------------------------------------------------------------+ //| Simulator of a deterministic complex quantum circuit | //| features[][0] – amplitude parameter, features[][1] – phase | //+------------------------------------------------------------------+ 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("Error: empty attribute matrix!"); return 0.0; } // A vector of quantum states based on the built-in `complex` type complex state[]; ArrayResize(state, num_states); // Initialise the system to a strict state |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; // The amplitude of the ground state is 1 // Temporary buffer for atomic (simultaneous) vector updates complex next_state[]; ArrayResize(next_state, num_states); // --- 1. Complex encoding of features (U-Gate: Ry + Rz) --- for(int qubit = 0; qubit < NUM_QUBITS; qubit++) { const int feat_idx = qubit % num_features; // Limit the input angles to the range [-PI; PI] const double theta = MathMax(MathMin(features_matrix[feat_idx][0] * M_PI, M_PI), -M_PI); // For Ry const double phi = MathMax(MathMin(features_matrix[feat_idx][1] * M_PI, M_PI), -M_PI); // For Rz const double cos_t = MathCos(theta / 2.0); const double sin_t = MathSin(theta / 2.0); // Euler’s exponential for phase shift: 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); // Complex rotation matrix U(theta, phi) of the valve: // [ cos(theta/2) , -sin(theta/2) ] // [ sin(theta/2)*e^(i*phi), cos(theta/2)*e^(i*phi) ] // Calculation for target bit = 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; // Calculation for target bit = 1 (taking into account the phase shift 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; // Apply a phase shift using native complex multiplication in MQL5 next_state[i1] = temp_i1 * e_phase; } } ArrayCopy(state, next_state); } // --- 2. Honest complex quantum entanglement (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; // We interweave the complex phases of two correlated parameters 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++) { // If both qubits are in state 1, we gradually rotate their shared complex phase if(((k & (1 << i)) != 0) && ((k & (1 << j)) != 0)) { state[k] = state[k] * cz_gate; } } } } // --- 3. Calculating exact theoretical probabilities (Deterministic step) --- double exact_probs[]; ArrayResize(exact_probs, num_states); for(int i = 0; i < num_states; i++) { // According to Born’s law, the probability P = |ψ|² = real² + imag² exact_probs[i] = (state[i].real * state[i].real) + (state[i].imag * state[i].imag); } // --- 4. Obtaining the final forecast --- const double prediction = GetWeightedVote(exact_probs); const double confidence = MathAbs(prediction); if(confidence < MIN_CONFIDENCE) { return 0.0; // The market is in a flat/indecisive phase from the perspective of the quantum chain } return prediction; }
- features[i][0] — amplitude parameter (specifies the real rotation angle (Ry)).
- features[i][1] — phase feature (specifies the complex rotation angle (Rz)).
- Free trading apps
- Over 8,000 signals for copying
- Economic news for exploring financial markets
You agree to website policy and terms of use
Check out the new article: From Python to MQL5: A Journey into Quantum-Inspired Trading Systems.
The article explores the development of a quantum-inspired trading system, transitioning from a Python prototype to an MQL5 implementation for real-world trading. The system uses quantum computing principles like superposition and entanglement to analyze market states, though it runs on classical computers using quantum simulators. Key features include a three-qubit system for analyzing eight market states simultaneously, 24-hour lookback periods, and seven technical indicators for market analysis. While the accuracy rates might seem modest, they provide a significant edge when combined with proper risk management strategies.
We'll go on a trip that connects theoretical ideas of quantum computing with real-world trading applications in this thorough investigation of quantum-inspired trading systems. Starting with basic quantum computing ideas and ending with a real-world MQL5 implementation, this tutorial is designed to walk you through the whole development process. We will discuss how trading might benefit from the use of quantum concepts, describe our development approach from the Python prototype to the integration of MQL5, and present real performance data and code implementations.
This article explores the application of quantum-inspired concepts in trading systems, bridging theoretical quantum computing with practical implementation in MQL5. We’ll introduce essential quantum principles and guide you from Python prototyping to MQL5 integration, with real-world performance data.
Unlike traditional trading, which relies on binary decision-making, quantum-inspired trading models capitalize on market behaviors similar to quantum phenomena—multiple concurrent states, interconnections, and abrupt state shifts. By using quantum simulators like Qiskit, we can apply quantum-inspired algorithms on classical computers to handle market uncertainty and generate predictive insights.
Author: Javier Santiago Gaston De Iriarte Cabrera