Amin-QVM: Quantum Computing Library
Project description
amin-qvm
..contents:: amin-qvm is a Python library for simulating quantum circuits and algorithms. It provides a simple interface for creating quantum circuits, applying quantum gates, measuring qubits, and analyzing results.
Getting Started
To use amin-qvm, first import the QuantumComputer class::
from amin_qvm import QuantumComputer
Then create an instance of the QuantumComputer::
qc = QuantumComputer()
This creates a quantum computer with the default number of qubits. You can also specify the number of qubits::
qc = QuantumComputer(5) # 5-qubit quantum computer
Operations
The QuantumComputer class provides methods for common quantum operations:
Applying Gates ^^^^^^^^^^^^^^ To apply a gate to a qubit, use the apply_gate method::
qc.apply_gate(Gate.H, 0) # Apply Hadamard gate to qubit 0
The supported single-qubit gates are X, Y, Z, H, T, S, and Sdg.
To apply a controlled gate between two qubits, use the apply_controlled_gate method::
qc.apply_controlled_gate(Gate.CX, 0, 1) # Apply CX (CNOT) gate with qubit 0 as control and qubit 1 as target
Measurement ^^^^^^^^^^^ To measure a qubit, use the measure method::
result = qc.measure(0)
print(result) # 0 or 1
This will collapse the qubit wavefunction and return the result.
Reset ^^^^^ To reset all qubits to the |0⟩ state, use the reset method::
qc.reset()
Visualization ^^^^^^^^^^^^^ To print a text representation of the quantum state, use the print_state method::
qc.print_state()
Circuits ^^^^^^^^ To build up circuits, apply a series of gates and measurements::
qc.apply_gate(Gate.H, 0)
qc.apply_gate(Gate.CX, 0, 1)
result = qc.measure(0)
print(result)
For larger circuits, you may want to write helper functions::
def bell_state_circuit(qc):
qc.apply_gate(Gate.H, 0)
qc.apply_gate(Gate.CX, 0, 1)
qc = QuantumComputer(2)
bell_state_circuit(qc)
Full Example ^^^^^^^^^^^^ Here is an example simulating a Bell state experiment::
from amin_qvm import QuantumComputer, Gate
qc = QuantumComputer(2)
# Apply gates
qc.apply_gate(Gate.H, 0)
qc.apply_gate(Gate.CX, 0, 1)
# Measure
m1 = qc.measure(0)
m2 = qc.measure(1)
print(m1, m2)
# Reset
qc.reset()
# Repeat experiment
m1 = qc.measure(0)
m2 = qc.measure(1)
print(m1, m2)
This allows you to quickly prototype quantum algorithms and analyze the results.
Using amin-qvm with Arduino
The amin-qvm library can interface with an Arduino microcontroller to generate random numbers from hardware sensors. This allows incorporating true randomness into quantum programs.
Arduino Code ^^^^^^^^^^^^ On the Arduino, read values from analog pins and print them to the serial port::
// Pins for the light sensors
int sensor1Pin = A0;
int sensor2Pin = A1;
void setup() {
Serial.begin(9600); // Start serial communication
}
void loop() {
// Read sensor values
int sensor1Value = analogRead(sensor1Pin);
int sensor2Value = analogRead(sensor2Pin);
// Normalize between 0 and 1
float sensor1Norm = sensor1Value / 1023.0;
float sensor2Norm = sensor2Value / 1023.0;
// Average them
float randomValue = (sensor1Norm + sensor2Norm) / 2.0;
// Send to Python
Serial.println(randomValue);
delay(100);
}
This reads two analog sensors, normalizes the values between 0 and 1, averages them, and prints to serial.
Python Code
^^^^^^^^^^^
In Python, import the microcontrollerconnectivity
module::
from amin_qvm import microcontrollerconnectivity as arduino
Then connect to the Arduino::
qc = QuantumComputer()
arduino.connect(qc)
This will automatically detect the Arduino port and baudrate.
To use the random values in a quantum program::
random_bit = arduino.get_random_bit(qc)
if random_bit:
qc.apply_gate(Gate.X, 0)
The get_random_bit
function returns 0 or 1 based on sampling the Arduino output.
Full Example ^^^^^^^^^^^^ Here is a full example::
from amin_qvm import QuantumComputer, Gate, microcontrollerconnectivity as arduino
qc = QuantumComputer(1)
arduino.connect(qc)
for i in range(10):
random_bit = arduino.get_random_bit(qc)
print(random_bit)
if random_bit:
qc.apply_gate(Gate.X, 0)
qc.measure(0)
This generates 10 random bits using the Arduino, applies X gates based on the bits, and measures the result.
Refer to the full documentation(coming soon) for more details on connecting to different ports or integrating randomness in other ways.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.