Quick Start¶
Get up and running with QSCI in minutes! This guide shows you how to run your first QSCI calculation.
Basic Example¶
Here's a complete example that runs VanillaQSCI on a simple 2-qubit Hamiltonian:
import numpy as np
from src.qsci_algorithms import VanillaQSCI
from quri_parts.core.operator import Operator, pauli_label
from quri_parts.core.state import GeneralCircuitQuantumState
from quri_parts.circuit import QuantumCircuit
from quri_parts.qulacs.sampler import create_qulacs_vector_concurrent_sampler
# Create a simple 2-qubit Hamiltonian
hamiltonian = Operator()
hamiltonian += Operator({pauli_label("Z0"): -1.25})
hamiltonian += Operator({pauli_label("Z1"): -1.25})
hamiltonian += Operator({pauli_label("Z0 Z1"): 0.5})
# Create uniform superposition initial state |++⟩
circuit = QuantumCircuit(2)
circuit.add_H_gate(0) # Apply Hadamard to qubit 0
circuit.add_H_gate(1) # Apply Hadamard to qubit 1
initial_state = GeneralCircuitQuantumState(2, circuit)
# Set up QSCI algorithm
sampler = create_qulacs_vector_concurrent_sampler()
qsci = VanillaQSCI(
hamiltonian=hamiltonian,
sampler=sampler,
num_states_pick_out=4 # Complete subspace for 2 qubits
)
# Run the algorithm
result = qsci.run([initial_state], total_shots=2000)
# Print results
print(f"Ground state energy: {result.ground_state_energy:.6f}")
print(f"All eigenvalues: {result.eigenvalues}")
print(f"Selected {len(result.selected_states)} configurations")
Expected output:
Ground state energy: -2.750000
All eigenvalues: [-2.75, -0.75, -0.75, 1.25]
Selected 4 configurations
TE-QSCI Example¶
Now let's try a time-evolved QSCI calculation:
from src.qsci_algorithms import SingleTimeTE_QSCI
# Same Hamiltonian and initial state as above
hamiltonian = Operator()
hamiltonian += Operator({pauli_label("Z0"): -1.25})
hamiltonian += Operator({pauli_label("Z1"): -1.25})
hamiltonian += Operator({pauli_label("Z0 Z1"): 0.5})
# Create initial state
circuit = QuantumCircuit(2)
circuit.add_H_gate(0)
circuit.add_H_gate(1)
initial_state = GeneralCircuitQuantumState(2, circuit)
# Set up TE-QSCI algorithm
sampler = create_qulacs_vector_concurrent_sampler()
te_qsci = SingleTimeTE_QSCI(
hamiltonian=hamiltonian,
sampler=sampler,
evolution_time=1.0, # Evolution time parameter
num_states_pick_out=4
)
# Run the algorithm
result = te_qsci.run([initial_state], total_shots=2000)
print(f"TE-QSCI Ground state energy: {result.ground_state_energy:.6f}")
Step-by-Step Explanation¶
1. Create the Hamiltonian¶
hamiltonian = Operator()
hamiltonian += Operator({pauli_label("Z0"): -1.25}) # σ_z^0 term
hamiltonian += Operator({pauli_label("Z1"): -1.25}) # σ_z^1 term
hamiltonian += Operator({pauli_label("Z0 Z1"): 0.5}) # σ_z^0 ⊗ σ_z^1 term
This creates the Hamiltonian:
2. Create the Initial State¶
circuit = QuantumCircuit(2)
circuit.add_H_gate(0) # |+⟩ = (|0⟩ + |1⟩)/√2
circuit.add_H_gate(1) # |+⟩ = (|0⟩ + |1⟩)/√2
initial_state = GeneralCircuitQuantumState(2, circuit)
This creates the uniform superposition state:
3. Configure the Algorithm¶
qsci = VanillaQSCI(
hamiltonian=hamiltonian,
sampler=sampler,
num_states_pick_out=4 # Use complete subspace
)
Key parameters:
- hamiltonian
: The quantum Hamiltonian to diagonalize
- sampler
: Quantum measurement simulator
- num_states_pick_out
: Number of configurations to select (4 = 2² for complete subspace)
4. Run and Analyze¶
The result contains:
- ground_state_energy
: Lowest eigenvalue
- eigenvalues
: All computed eigenvalues
- selected_states
: Configurations used in the calculation
More Examples¶
Time-Average TE-QSCI¶
from src.qsci_algorithms import TimeAverageTE_QSCI
te_qsci_avg = TimeAverageTE_QSCI(
hamiltonian=hamiltonian,
sampler=sampler,
evolution_times=[0.5, 1.0, 1.5], # Multiple time points
num_states_pick_out=4
)
result = te_qsci_avg.run([initial_state], total_shots=3000)
print(f"Time-average energy: {result.ground_state_energy:.6f}")
State Vector TE-QSCI¶
from src.qsci_algorithms import StateVectorTE_QSCI
te_qsci_exact = StateVectorTE_QSCI(
hamiltonian=hamiltonian,
sampler=sampler,
evolution_time=1.0,
num_states_pick_out=4
)
result = te_qsci_exact.run([initial_state], total_shots=0) # No shots needed
print(f"Exact energy: {result.ground_state_energy:.10f}")
Best Practices¶
1. Use Uniform Superposition¶
For optimal performance, always use uniform superposition initial states:
def create_uniform_superposition(n_qubits):
circuit = QuantumCircuit(n_qubits)
for i in range(n_qubits):
circuit.add_H_gate(i)
return GeneralCircuitQuantumState(n_qubits, circuit)
2. Complete Subspace Coverage¶
For small systems, use complete subspace coverage:
3. Sufficient Shot Counts¶
Use adequate shot counts for stable results:
4. Validate Results¶
Always validate against exact solutions for small systems:
from scipy.sparse.linalg import eigsh
# Convert to matrix and solve exactly
H_matrix = hamiltonian_to_matrix(hamiltonian) # Helper function
exact_energy, _ = eigsh(H_matrix, k=1, which='SA')
print(f"QSCI energy: {result.ground_state_energy:.6f}")
print(f"Exact energy: {exact_energy[0]:.6f}")
print(f"Error: {abs(result.ground_state_energy - exact_energy[0]):.2e}")
Common Issues¶
1. Import Errors¶
Make sure QURI Parts is installed:
2. Numerical Precision¶
Use appropriate tolerances for comparisons:
3. Memory Issues¶
For large systems, reduce the number of selected states:
Next Steps¶
Now that you've run your first QSCI calculation:
- Algorithm Overview - Learn about different QSCI variants
- Examples - Explore more complex examples
- Testing - Understand the validation framework
- API Reference - Dive into the technical details
Interactive Examples¶
Try these examples in a Jupyter notebook:
- H2 Molecule - Quantum chemistry application
- TFIM Models - Condensed matter physics
- Performance Analysis - Scaling and optimization