-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add initial project structure with harmonic balancer implementation, …
…requirements, and demo interface
- Loading branch information
Showing
14 changed files
with
757 additions
and
144 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
from flask import Flask, render_template, request, jsonify | ||
from src.harmonic_balancer import EnhancedHarmonicBalancer | ||
import numpy as np | ||
|
||
app = Flask(__name__) | ||
|
||
@app.route('/') | ||
def index(): | ||
return render_template('index.html') | ||
|
||
@app.route('/api/balance', methods=['POST']) | ||
def balance(): | ||
data = request.json | ||
base_freq = data['baseFreq'] | ||
harmonic_level = data['harmonicLevel'] | ||
|
||
# Generate a sample signal | ||
t = np.linspace(0, 1, 1000) | ||
signal = np.sin(2 * np.pi * base_freq * t) + (harmonic_level / 100) * np.sin(2 * np.pi * 2 * base_freq * t) | ||
|
||
# Process the signal | ||
balancer = EnhancedHarmonicBalancer(base_freq, num_harmonics=5, application='power') | ||
balanced_signal = balancer.balance_signal(signal, sample_rate=1000) | ||
|
||
# Calculate THD before and after | ||
thd_before = balancer.calculate_thd(signal, 1000) | ||
thd_after = balancer.calculate_thd(balanced_signal, 1000) | ||
|
||
# Prepare data for plotting | ||
return jsonify({ | ||
'signals': [ | ||
{'x': t.tolist(), 'y': signal.tolist(), 'type': 'scatter', 'name': 'Original'}, | ||
{'x': t.tolist(), 'y': balanced_signal.tolist(), 'type': 'scatter', 'name': 'Balanced'} | ||
], | ||
'layout': {'title': 'Signal Comparison'}, | ||
'spectrum': [ | ||
{'x': np.fft.fftfreq(1000, 1/1000)[:500].tolist(), 'y': np.abs(np.fft.fft(signal))[:500].tolist(), 'type': 'scatter', 'name': 'Original Spectrum'}, | ||
{'x': np.fft.fftfreq(1000, 1/1000)[:500].tolist(), 'y': np.abs(np.fft.fft(balanced_signal))[:500].tolist(), 'type': 'scatter', 'name': 'Balanced Spectrum'} | ||
], | ||
'spectrumLayout': {'title': 'Frequency Spectrum'}, | ||
'metrics': { | ||
'thdBefore': f"{thd_before:.2%}", | ||
'thdAfter': f"{thd_after:.2%}", | ||
'improvement': f"{(thd_before - thd_after) / thd_before:.2%}" | ||
} | ||
}) | ||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
body { | ||
font-family: Arial, sans-serif; | ||
margin: 0; | ||
padding: 20px; | ||
background-color: #f0f0f0; | ||
} | ||
|
||
.container { | ||
max-width: 1200px; | ||
margin: 0 auto; | ||
background-color: white; | ||
padding: 20px; | ||
border-radius: 10px; | ||
box-shadow: 0 0 10px rgba(0,0,0,0.1); | ||
} | ||
|
||
h1 { | ||
text-align: center; | ||
color: #333; | ||
} | ||
|
||
.control-panel { | ||
display: flex; | ||
justify-content: space-between; | ||
align-items: center; | ||
margin-bottom: 20px; | ||
} | ||
|
||
.parameters { | ||
display: flex; | ||
gap: 20px; | ||
} | ||
|
||
.visualization { | ||
display: flex; | ||
gap: 20px; | ||
margin-bottom: 20px; | ||
} | ||
|
||
#signalPlot, #spectrumPlot { | ||
flex: 1; | ||
height: 400px; | ||
} | ||
|
||
.metrics { | ||
display: flex; | ||
justify-content: space-around; | ||
} | ||
|
||
.metric-box { | ||
text-align: center; | ||
background-color: #f9f9f9; | ||
padding: 10px; | ||
border-radius: 5px; | ||
} | ||
|
||
.metric-box h3 { | ||
margin: 0; | ||
color: #666; | ||
} | ||
|
||
.metric-box span { | ||
font-size: 24px; | ||
font-weight: bold; | ||
color: #333; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8"> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0"> | ||
<title>Harmonic Balancer Demo</title> | ||
<link rel="stylesheet" href="{{ url_for('static', filename='css/style.css') }}"> | ||
<script src="https://cdn.plot.ly/plotly-latest.min.js"></script> | ||
</head> | ||
<body> | ||
<div class="container"> | ||
<h1>Harmonic Balancer Demonstration</h1> | ||
<div class="control-panel"> | ||
<button id="startDemo">Start Demo</button> | ||
<div class="parameters"> | ||
<label>Base Frequency: <input type="number" id="baseFreq" value="60" min="1" max="400"></label> | ||
<label>Harmonic Level: <input type="range" id="harmonicLevel" min="0" max="100" value="30"></label> | ||
</div> | ||
</div> | ||
<div class="visualization"> | ||
<div id="signalPlot"></div> | ||
<div id="spectrumPlot"></div> | ||
</div> | ||
<div class="metrics"> | ||
<div class="metric-box"> | ||
<h3>THD Before</h3> | ||
<span id="thdBefore">0%</span> | ||
</div> | ||
<div class="metric-box"> | ||
<h3>THD After</h3> | ||
<span id="thdAfter">0%</span> | ||
</div> | ||
<div class="metric-box"> | ||
<h3>Improvement</h3> | ||
<span id="improvement">0%</span> | ||
</div> | ||
</div> | ||
</div> | ||
<script src="{{ url_for('static', filename='js/demo.js') }}"></script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
document.addEventListener('DOMContentLoaded', function() { | ||
const startButton = document.getElementById('startDemo'); | ||
const baseFreqInput = document.getElementById('baseFreq'); | ||
const harmonicLevelInput = document.getElementById('harmonicLevel'); | ||
|
||
function updatePlots() { | ||
fetch('/api/balance', { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
baseFreq: parseFloat(baseFreqInput.value), | ||
harmonicLevel: parseFloat(harmonicLevelInput.value) | ||
}), | ||
}) | ||
.then(response => response.json()) | ||
.then(data => { | ||
// Update plots | ||
Plotly.newPlot('signalPlot', data.signals, data.layout); | ||
Plotly.newPlot('spectrumPlot', data.spectrum, data.spectrumLayout); | ||
|
||
// Update metrics | ||
document.getElementById('thdBefore').textContent = data.metrics.thdBefore; | ||
document.getElementById('thdAfter').textContent = data.metrics.thdAfter; | ||
document.getElementById('improvement').textContent = data.metrics.improvement; | ||
}); | ||
} | ||
|
||
startButton.addEventListener('click', updatePlots); | ||
baseFreqInput.addEventListener('change', updatePlots); | ||
harmonicLevelInput.addEventListener('input', updatePlots); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
numpy>=1.18.0 | ||
scipy>=1.4.0 | ||
matplotlib>=3.1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .harmonic_balancer import EnhancedHarmonicBalancer |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
|
||
import numpy as np | ||
from qiskit import QuantumCircuit, QuantumRegister, ClassicalRegister | ||
|
||
class QuantumResonanceCircuit: | ||
def __init__(self, resonance_freq=4.40e9, coupling_strength=0.1): | ||
self.resonance_freq = resonance_freq | ||
self.coupling_strength = coupling_strength | ||
self.num_qubits = 4 | ||
|
||
def initialize_state(self): | ||
"""Initialize the quantum state""" | ||
return np.array([1.0] + [0.0] * (2**self.num_qubits - 1), dtype=complex) | ||
|
||
def get_hamiltonian(self): | ||
"""Calculate the Hamiltonian of the system""" | ||
dim = 2**self.num_qubits | ||
H = np.zeros((dim, dim), dtype=complex) | ||
# Add resonant coupling terms | ||
for i in range(self.num_qubits-1): | ||
H[i,i+1] = self.coupling_strength | ||
H[i+1,i] = self.coupling_strength | ||
# Add energy terms | ||
for i in range(dim): | ||
H[i,i] = self.resonance_freq * bin(i).count('1') | ||
return H | ||
|
||
def evolve_state(self, state, time): | ||
"""Evolve the quantum state over time""" | ||
H = self.get_hamiltonian() | ||
U = np.exp(-1j * H * time) | ||
return U @ state | ||
|
||
def calculate_resonance_function(self, x, y): | ||
""" | ||
Calculate the resonance function f(x,y) for a pair of qubits | ||
""" | ||
return np.exp(-((x - y)**2) / (2 * self.coupling_strength)) | ||
|
||
def calculate_evolutionary_potential(self, x, y): | ||
""" | ||
Calculate the evolutionary potential P(x,y) for a pair of qubits | ||
""" | ||
resonance = self.calculate_resonance_function(x, y) | ||
return resonance * np.exp(-1j * self.resonance_freq * (x + y)) | ||
|
||
def get_total_possibilities(self): | ||
""" | ||
Calculate the total number of possibilities using the combined formula: | ||
T = ∑(i=1 to n)∑(j=i+1 to n) f(x_i, y_j) · P(x_i, y_j) | ||
""" | ||
total = 0 | ||
for i in range(self.num_qubits): | ||
for j in range(i + 1, self.num_qubits): | ||
f_xy = self.calculate_resonance_function(i, j) | ||
p_xy = self.calculate_evolutionary_potential(i, j) | ||
total += f_xy * np.abs(p_xy) | ||
return total | ||
|
||
def create_entangled_circuit(self): | ||
""" | ||
Create a quantum circuit with entangled pairs | ||
""" | ||
qr = QuantumRegister(self.num_qubits, 'q') | ||
cr = ClassicalRegister(self.num_qubits, 'c') | ||
qc = QuantumCircuit(qr, cr) | ||
|
||
# Create entangled pairs | ||
for i in range(0, self.num_qubits - 1, 2): | ||
qc.h(qr[i]) | ||
qc.cx(qr[i], qr[i+1]) | ||
|
||
return qc |
Oops, something went wrong.