more improvements
This commit is contained in:
parent
421fb17062
commit
f3e9e420a7
5 changed files with 48 additions and 27 deletions
|
|
@ -43,6 +43,7 @@
|
||||||
ppkgs.tqdm
|
ppkgs.tqdm
|
||||||
ppkgs.qiskit
|
ppkgs.qiskit
|
||||||
ppkgs.qiskit-aer
|
ppkgs.qiskit-aer
|
||||||
|
ppkgs.matplotlib
|
||||||
]))
|
]))
|
||||||
];
|
];
|
||||||
};
|
};
|
||||||
|
|
|
||||||
BIN
src/best_circuits.png
Normal file
BIN
src/best_circuits.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 22 KiB |
|
|
@ -3,24 +3,22 @@
|
||||||
# "Genetic optimization of ansatz expressibility for enhanced variational quantum algorithm performance"
|
# "Genetic optimization of ansatz expressibility for enhanced variational quantum algorithm performance"
|
||||||
|
|
||||||
import random
|
import random
|
||||||
from quantum_circuit import (
|
|
||||||
Gate,
|
import matplotlib.pyplot as plt
|
||||||
GateType,
|
|
||||||
QuantumCircuit,
|
|
||||||
circ_from_layers,
|
|
||||||
sample_random_generator,
|
|
||||||
)
|
|
||||||
from qas_flow import Stream
|
from qas_flow import Stream
|
||||||
|
from quantum_circuit import (Gate, GateType, QuantumCircuit, circ_from_layers,
|
||||||
|
sample_random_generator, single_typ)
|
||||||
|
|
||||||
DEPTH = 20
|
DEPTH: int = 6
|
||||||
QUBITS = 5
|
QUBITS: int = 6
|
||||||
GENERATIONS = 20
|
GENERATIONS: int = 40
|
||||||
GENERATION_SIZE = 20
|
GENERATION_SIZE: int = 60
|
||||||
PARENT_AMOUNT = 5
|
PARENT_AMOUNT: int = 10
|
||||||
MUTATION_RATE = 0.1
|
MUTATION_RATE: float = 0.1
|
||||||
|
|
||||||
|
|
||||||
gate_set = [
|
gate_set: list[GateType] = [
|
||||||
GateType.H,
|
GateType.H,
|
||||||
GateType.RX,
|
GateType.RX,
|
||||||
GateType.RY,
|
GateType.RY,
|
||||||
|
|
@ -30,8 +28,15 @@ gate_set = [
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def plot_best_circuits(best_circuits: list[QuantumCircuit]) -> None:
|
||||||
seed_rng = random.Random(1020381)
|
fig, ax = plt.subplots()
|
||||||
|
|
||||||
|
ax.plot([-circ.expressibility for circ in best_circuits])
|
||||||
|
fig.savefig("best_circuits.png")
|
||||||
|
|
||||||
|
|
||||||
|
def main() -> None:
|
||||||
|
seed_rng: random.Random = random.Random(1020381)
|
||||||
initial_population: list[QuantumCircuit] = (
|
initial_population: list[QuantumCircuit] = (
|
||||||
Stream(sample_random_generator(random.Random(101020), QUBITS, DEPTH, gate_set))
|
Stream(sample_random_generator(random.Random(101020), QUBITS, DEPTH, gate_set))
|
||||||
.apply(lambda circ: print(circ))
|
.apply(lambda circ: print(circ))
|
||||||
|
|
@ -49,6 +54,8 @@ def main():
|
||||||
|
|
||||||
main_rng = random.Random(2837175)
|
main_rng = random.Random(2837175)
|
||||||
|
|
||||||
|
best_circuits: list[QuantumCircuit] = []
|
||||||
|
|
||||||
for generation in range(GENERATIONS):
|
for generation in range(GENERATIONS):
|
||||||
print(f"starting generation {generation}")
|
print(f"starting generation {generation}")
|
||||||
population.sort(key=lambda qc: qc.expressibility, reverse=True)
|
population.sort(key=lambda qc: qc.expressibility, reverse=True)
|
||||||
|
|
@ -68,22 +75,35 @@ def main():
|
||||||
|
|
||||||
if old_gate.single():
|
if old_gate.single():
|
||||||
child_layers[layer_idx][gate_idx] = Gate(
|
child_layers[layer_idx][gate_idx] = Gate(
|
||||||
main_rng.choice(gate_set),
|
main_rng.choice(
|
||||||
|
[gate for gate in gate_set if single_typ(gate)]
|
||||||
|
),
|
||||||
old_gate.qubits,
|
old_gate.qubits,
|
||||||
old_gate.param_idx,
|
old_gate.param_idx,
|
||||||
)
|
)
|
||||||
else:
|
else:
|
||||||
child_layers[layer_idx][gate_idx] = Gate(
|
child_layers[layer_idx][gate_idx] = Gate(
|
||||||
old_gate.typ,
|
old_gate.typ,
|
||||||
tuple(old_gate.qubits[::-1]),
|
(old_gate.qubits[1], old_gate.qubits[0]),
|
||||||
old_gate.param_idx,
|
old_gate.param_idx,
|
||||||
)
|
)
|
||||||
|
|
||||||
child = circ_from_layers(child_layers, QUBITS)
|
child = circ_from_layers(child_layers, QUBITS)
|
||||||
child.expressibility_estimate(2000, seed_rng.randint(1000, 1000000000))
|
child.expressibility_estimate(2000, seed_rng.randint(1000, 1000000000))
|
||||||
offspring.append(child)
|
offspring.append(child)
|
||||||
|
|
||||||
|
offspring.sort(key=lambda qc: qc.expressibility, reverse=True)
|
||||||
|
if population[0].expressibility > offspring[0].expressibility:
|
||||||
|
print(f"best parent > best child")
|
||||||
|
best_circuits.append(population[0])
|
||||||
|
else:
|
||||||
|
print(f"best child > best parent")
|
||||||
|
best_circuits.append(offspring[0])
|
||||||
population = offspring
|
population = offspring
|
||||||
|
|
||||||
|
plot_best_circuits(best_circuits)
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
|
||||||
|
|
@ -1,11 +1,12 @@
|
||||||
from dataclasses import dataclass
|
|
||||||
import math
|
import math
|
||||||
import random
|
import random
|
||||||
from typing import override
|
from dataclasses import dataclass
|
||||||
import numpy as np
|
|
||||||
from enum import IntEnum
|
from enum import IntEnum
|
||||||
|
from typing import override
|
||||||
|
|
||||||
from qiskit import QuantumCircuit as QiskitCircuit, transpile
|
import numpy as np
|
||||||
|
from qiskit import QuantumCircuit as QiskitCircuit
|
||||||
|
from qiskit import transpile
|
||||||
from qiskit.circuit import ParameterVector, ParameterVectorElement
|
from qiskit.circuit import ParameterVector, ParameterVectorElement
|
||||||
from qiskit_aer import AerSimulator
|
from qiskit_aer import AerSimulator
|
||||||
|
|
||||||
|
|
@ -369,11 +370,10 @@ def sample_circuit_random(
|
||||||
for loc in range(qubits):
|
for loc in range(qubits):
|
||||||
if loc in used_qubits:
|
if loc in used_qubits:
|
||||||
continue
|
continue
|
||||||
gate_type = rng.choice(
|
if loc + 1 < qubits:
|
||||||
gate_types
|
gate_type = rng.choice(gate_types)
|
||||||
if loc + 1 < qubits
|
else:
|
||||||
else [typ for typ in gate_types if single_typ(typ)]
|
gate_type = rng.choice([typ for typ in gate_types if single_typ(typ)])
|
||||||
)
|
|
||||||
match gate_type:
|
match gate_type:
|
||||||
case GateType.H:
|
case GateType.H:
|
||||||
layer.append(Gate(gate_type, loc, params))
|
layer.append(Gate(gate_type, loc, params))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue