PhaseEstimation package
Submodules
PhaseEstimation.annni_model module
This module implements the base functions for treating the Hamiltonian of the ANNNI Ising-model
- PhaseEstimation.annni_model.build_Hs(N: int, n_hs: int, n_kappas: int, h_max: float = 2, kappa_max: float = 1, ring: bool = False) Tuple[List[Hamiltonian], List[List[int]], List[int], List[Tuple[int, float, float]], int][source]
Sets up np.ndarray of pennylane Hamiltonians with different parameters total_states = n_kappas * n_hs kappa can have n_kappas values from 0 to - abs(kappa_max) (NB the sign) h can have n_hs values from 0 to h_max
- Parameters:
N (int) – Number of spins of the Ising Chain
n_hs (int) – Number of different values of h the hamiltonian can have
h_max (float) – Maximum value of h, the values will range from 0 to h_max
n_kappas (int) – Number of different values of kappa the hamiltonian can have
kappa_max (float) – Maximum value of kappa
ring (bool) – If False, system has open-boundaries condition
- Returns:
np.array – Array of pennylane Hamiltonians
np.array – Array of labels for analytical solutions
np.array – Array for the recycle rule
np.array – Array for the states parameters
- PhaseEstimation.annni_model.get_H(N: int, L: float, K: float, ring: bool = False) Hamiltonian[source]
- Set up Hamiltonian:
H = J1* (- Σsigma^i_x*sigma_x^{i+1} - (h/J1) * Σsigma^i_z - (J2/J1) * Σsigma^i_x*sigma_x^{i+2} )
[where J1 = 1, (h/J1) = Lambda(/L), (J2/J1) = K]
- Parameters:
N (int) – Number of spins of the Ising Chain
L (float) – h/J1 parameter
K (float) – J1/J2 parameter
ring (bool) – If False, system has open-boundaries condition
- Returns:
Hamiltonian Pennylane class for the (Transverse) Ising Chain
- Return type:
pennylane.ops.qubit.hamiltonian.Hamiltonian
PhaseEstimation.circuits module
This module implements base circuit layouts for all the models used
- PhaseEstimation.circuits.circuit_ID9(active_wires: List[int], params: List[Number], index: int = 0) int[source]
Basic block for VQE
- Parameters:
active_wires (np.ndarray) – Array of the wires to apply the rotations to
params (list) – List of parameters of the whole circuit
index (int) – Starting index for this block of operators
- Returns:
Updated index value
- Return type:
int
- PhaseEstimation.circuits.convolution(active_wires: List[int], params: List[Number], index: int = 0) int[source]
Convolution block for the QCNN
- Parameters:
active_wires (np.ndarray) – Array of wires that are not measured during a previous pooling
params (np.ndarray) – Array of parameters/rotation for the circuit
index (int) – Index from where to pick the elements from the params array
- Returns:
Updated starting index of params array for further rotations
- Return type:
int
- PhaseEstimation.circuits.encoder_block(wires: List[int], wires_trash: List[int], shift: int = 0)[source]
Applies CX between a wire and a trash wire for each wire/trashwire
- Parameters:
wires (np.ndarray) – Array of the indexes of non-trash qubits
wires_trash (np.ndarray) – Array of the indexes of trash qubits (np.1dsetdiff(np.arange(N),wires))
shift (int) – Shift value for connections between wires and trash wires
- PhaseEstimation.circuits.encoder_circuit(wires: List[int], wires_trash: List[int], active_wires: List[int], params: List[Number], index: int = 0) int[source]
Encoder circuit for encoder and autoencoder
- Parameters:
wires (np.ndarray) – Array of the indexes of non-trash qubits
wires_trash (np.ndarray) – Array of the indexes of trash qubits (np.1dsetdiff(np.arange(N),wires))
active_wires (np.ndarray) – wires U wires_trash
params (np.ndarray) – Array of parameters/rotation for the circuit
index (int) – Index from where to pick the elements from the params array
- Returns:
Updated index value
- Return type:
int
- PhaseEstimation.circuits.pooling(active_wires: List[int], qmlrot_func: Operator, params: List[Number], index: int = 0) Tuple[int, List[int]][source]
Pooling block for the QCNN
- Parameters:
active_wires (np.ndarray) – Array of wires that are not measured during a previous pooling
qmlrot_func (function) – Pennylane Gate function to apply
params (np.ndarray) – Array of parameters/rotation for the circuit
index (int) – Index from where to pick the elements from the params array
- Returns:
int – Updated starting index of params array for further rotations
np.ndarray – Updated array of active wires (not measured)
- PhaseEstimation.circuits.wall_cgate_all(active_wires: List[int], cgate: Operator, params: List[Number] = [], index: int = 0, going_down: bool = True) int[source]
Apply controlled rotations across all the wires (active_wires)
- Parameters:
active_wires (np.ndarray) – Array of the wires to apply the rotations to
cgate (Pennylane gate (parametrized or not)) – Qubit controlled operator to apply
params (list) – List of parameters of the whole circuit
index (int) – Starting index for this block of operators
going_down (bool) – if True -> top - down, if False -> down - top
- Returns:
Updated index value
- Return type:
int
- PhaseEstimation.circuits.wall_cgate_nextneighbour(active_wires: List[int], cgate: Operator, params: List[Number] = [], index: int = 0, going_down: bool = True) int[source]
Apply drop-down controlled rotations establishing next-neighbour entanglement
- Parameters:
active_wires (np.ndarray) – Array of the wires to apply the rotations to
cgate (Pennylane gate (parametrized or not)) – Qubit controlled operator to apply
params (list) – List of parameters of the whole circuit
index (int) – Starting index for this block of operators
going_down (bool) – if True -> top - down, if False -> down - top
- Returns:
Updated index value
- Return type:
int
- PhaseEstimation.circuits.wall_cgate_serial(active_wires: List[int], cgate: Operator, params: List[Number] = [], index: int = 0, going_down: bool = True) int[source]
Apply drop-down controlled rotations for all the wires (active_wires)
- Parameters:
active_wires (np.ndarray) – Array of the wires to apply the rotations to
cgate (Pennylane gate (parametrized or not)) – Qubit controlled operator to apply
params (list) – List of parameters of the whole circuit
index (int) – Starting index for this block of operators
going_down (bool) – if True -> top - down, if False -> down - top
- Returns:
Updated index value
- Return type:
int
- PhaseEstimation.circuits.wall_gate(active_wires: List[int], gate: Operator, params: List[Number] = [], index: int = 0, samerot: bool = False) int[source]
Apply rotations for all the wires (active_wires)
- Parameters:
active_wires (np.ndarray) – Array of the wires to apply the rotations to
gate (qml.ops.qubit.parametric_ops) – Qubit operator to apply
params (list) – List of parameters of the whole circuit
index (int) – Starting index for this block of operators
samerot (bool) – if True -> The rotations are not independet of each other but the same
- Returns:
Updated index value
- Return type:
int
PhaseEstimation.encoder module
This module implements the base functions to implement an anomaly detector model
- PhaseEstimation.encoder.enc_classification_ANNNI(vqeclass: vqe, lr: Number, epochs: int) List[Number][source]
Train 3 encoder on the corners: > K = 0, L = 2 (Paramagnetic) > K = 0, L = 0 (Ferromagnetic) > K = -1, L = 0 (Antiphase) The other states will be classified taking the lowest error among each encoder
- Parameters:
vqeclass (class) – VQE class
lr (float) – Learning rate for each training
epochs (int) – Number of epochs for each training
- Returns:
Array of labels
- Return type:
np.ndarray
- class PhaseEstimation.encoder.encoder(vqe: vqe, encoder_circuit: Callable)[source]
Bases:
object- show_compression(trainingpoint, label=False, plot3d=False)[source]
Plots performance of the compression on the whole data for an encoder on the ANNI model
- Parameters:
trainingpoint (int) – Mark the single training point on the plot
label (str) – Label to assign to the picture, needed for the paper
plot3d (bool) – If True the 3D plot will be displayed aswell
- train(lr: Number, n_epochs: int, train_index: List[int], circuit: bool = False)[source]
Training function for the Anomaly Detector.
- Parameters:
lr (float) – Learning rate to be multiplied in the circuit-gradient output
n_epochs (int) – Total number of epochs for each learning
train_index (np.ndarray) – Index of training points
circuit (bool) – if True -> Prints the circuit
- PhaseEstimation.encoder.encoder_circuit(N: int, params: List[Number]) int[source]
Building function for the circuit Encoder(params)
- Parameters:
N (int) – Number of qubits
params (np.ndarray) – Array of parameters/rotation for the circuit
- Returns:
Number of parameters of the circuit
- Return type:
int
PhaseEstimation.general module
Module for generic functions for other modules
- PhaseEstimation.general.get_H_eigval_eigvec(qml_H: Hamiltonian, en_lvl: int) Tuple[List[List[Number]], Number, List[Number]][source]
Function for getting the energy value and state of an Ising Hamiltonian using the jitted jnp.linalg.eigh function
- Parameters:
qml_H (pennylane.ops.qubit.hamiltonian.Hamiltonian) – Pennylane Hamiltonian of the state
en_lvl (int) – Energy level desired
- Returns:
np.ndarray – Matricial encoding of the Hamiltonian
float – Value of the energy level
np.ndarray – Eigenstate of the energy level
- PhaseEstimation.general.get_VQD_params(qml_H: Hamiltonian, beta: Number) Tuple[List[List[Number]], List[List[Number]], Number][source]
Function for getting all the training parameter for the VQD algorithm for finding the first excited state
- Parameters:
qml_H (pennylane.ops.qubit.hamiltonian.Hamiltonian) – Pennylane Hamiltonian of the Ising Model
- Returns:
np.ndarray – Matricial encoding of the Hamiltonian
np.ndarray – Effective Hamiltonian of VQD algorithm
float – Excited-state energy value
- PhaseEstimation.general.get_VQE_params(qml_H: Hamiltonian) Tuple[List[List[Number]], Number][source]
Function for getting all the training parameter for the VQE algorithm
- Parameters:
qml_H (pennylane.ops.qubit.hamiltonian.Hamiltonian) – Pennylane Hamiltonian of the state
- Returns:
np.ndarray – Matricial encoding of the Hamiltonian
float – Ground-state energy value
- PhaseEstimation.general.geteigvals(qml_H: Hamiltonian, states: List[int]) List[Number][source]
Function for getting the energy values of an Ising Hamiltonian using the jitted jnp.linalg.eigh function
- Parameters:
qml_H (pennylane.ops.qubit.hamiltonian.Hamiltonian) – Pennylane Hamiltonian of the state
states (list) – List of energy levels desired
- Returns:
List of energy values
- Return type:
list
- PhaseEstimation.general.j_linalgeigh(mat_H: List[List[Number]]) Tuple[int, List[Number]]
Apply jax np.linalg.eigh on a matrix, to be jitted
- Parameters:
mat_H (np.ndarray) – Input matrix to apply np.linalg.eigh
- Returns:
np.ndarray – Array of eigenvalues (not sorted)
np.ndarray – Array of relative eigenvectors
- PhaseEstimation.general.j_psi_outer(psi: List[Number]) List[List[Number]]
- PhaseEstimation.general.jv_psi_outer(psi: List[Number]) List[List[Number]]
Vectorized version of psi_outer. Takes similar arguments as psi_outer but with additional array axes over which psi_outer is mapped.
- PhaseEstimation.general.linalgeigh(mat_H: List[List[Number]]) Tuple[int, List[Number]][source]
Apply jax np.linalg.eigh on a matrix, to be jitted
- Parameters:
mat_H (np.ndarray) – Input matrix to apply np.linalg.eigh
- Returns:
np.ndarray – Array of eigenvalues (not sorted)
np.ndarray – Array of relative eigenvectors
PhaseEstimation.hamiltonians module
This module implements the base class for spin-models Hamiltonians
- PhaseEstimation.hamiltonians.get_e_psi(Hclass, en_lvl)[source]
Return respectively the list of the true energies and true states obtained through the diagonalization of the hamiltonian matrices
- Parameters:
Hclass (hamiltonians.hamiltonians) – Custom hamiltonian class
en_lvl (int) – Energy level to inspect
- Returns:
List[Number] – Array of the energies
List[List[Number]] – Array of the state vectors
- class PhaseEstimation.hamiltonians.hamiltonian(building_func: Callable, **kwargs)[source]
Bases:
object- J: float
- N: int
- add_true()[source]
Add true ground-state energy levels and true wavefunctions by diagonalizing the Hamiltonian matrices
- show_massgap(**kwargs)[source]
Shows the mass gap which is defined as the difference between the first excited leven and the ground energy level for each point in the parameter space.
- Parameters:
Hs (hamiltonians.hamiltonian) – Custom hamiltonian class, it is needed to call plot_layout
phase_lines (bool) – if True plots the phase transition lines
pe_line (bool) – if True plots Peshel Emery line
PhaseEstimation.ising_chain module
This module implements the base function for treating Ising Chain with Transverse Field.
- PhaseEstimation.ising_chain.build_Hs(N: int, J: float, n_states: int, ring: bool = False) Tuple[List[Hamiltonian], List[int], List[int], List[Tuple[int, Number, Number]], int][source]
Sets up np.ndarray of pennylane Hamiltonians with different instensity of magnetic field mu in np.linspace(0, 2*J, n_states)
- Parameters:
N (int) – Number of spins of the Ising Chain
J (float) – Interaction strenght between spins
n_states (int) – Number of Hamiltonians to generate
ring (bool) – If False, system has open-boundaries condition
- Returns:
np.array – Array of pennylane Hamiltonians
np.array – Array of labels for analytical solutions
np.array – Array for the recycle rule
np.array – Array for the states parameters
int – Number of states
- PhaseEstimation.ising_chain.get_H(N: int, lam: float, J: float, ring: bool = False) Hamiltonian[source]
- Set up Hamiltonian:
H = -lam*Σsigma^i_z - J*Σsigma^i_x*sigma_x^{i+1}
- Parameters:
N (int) – Number of spins of the Ising Chain
lam (float) – Strenght of (transverse) magnetic field
J (float) – Interaction strenght between spins
ring (bool) – If False, system has open-boundaries condition
- Returns:
Hamiltonian Pennylane class for the (Transverse) Ising Chain
- Return type:
pennylane.ops.qubit.hamiltonian.Hamiltonian
PhaseEstimation.losses module
This module implements loss functions and regularizers for VQE, QCNN and Encoder
- PhaseEstimation.losses.cross_entropy(X, Y, params, q_circuit)[source]
LOSS: Compute Cross Entropy for a binary classification task
- Parameters:
X (np.ndarray) – Array of VQE parameters (input of VQE)
Y (np.ndarray) – Array of labels
params (np.ndarray) – Array of parameters of the QCNN circuit
q_circuit (function) – Quantum function of the VQE circuit
- Returns:
Cross entropy <Circuit(X)|Y>
- Return type:
float
- PhaseEstimation.losses.cross_entropy1D(X, Y, params, q_circuit)[source]
LOSS: Compute Cross Entropy for a binary classification task
- Parameters:
X (np.ndarray) – Array of VQE parameters (input of VQE)
Y (np.ndarray) – Array of labels
params (np.ndarray) – Array of parameters of the QCNN circuit
q_circuit (fun) – Quantum function of the VQE circuit
- Returns:
Cross entropy <Circuit(X)|Y>
- Return type:
float
- PhaseEstimation.losses.cross_entropy_power4(X, Y, params, q_circuit)[source]
LOSS: Compute Cross Entropy for a binary classification task Apply ^4 to punish the model on uncertain classifications
- Parameters:
X (np.ndarray) – Array of VQE parameters (input of VQE)
Y (np.ndarray) – Array of labels
params (np.ndarray) – Array of parameters of the QCNN circuit
q_circuit (function) – Quantum function of the VQE circuit
- Returns:
Cross entropy <Circuit(X)|Y>
- Return type:
float
- PhaseEstimation.losses.hinge(X, Y, params, q_circuit)[source]
LOSS: (Experimental) Compute Hinge loss for a binary classification task N.B: MAX is not applied because output is a probability [0,1] that will be mapped to [-1,1], hence the 1 - Prediction(X)*Y can be at minimum 0
- Parameters:
X (np.ndarray) – Array of VQE parameters (input of VQE)
Y (np.ndarray) – Array of labels
params (np.ndarray) – Array of parameters of the QCNN circuit
q_circuit (fun) – Quantum function of the VQE circuit
- Returns:
Mean Hinge Loss <Circuit(X)|Y>
- Return type:
float
- PhaseEstimation.losses.vqe_fidelities(Y: List[Number], params: List[Number], q_circuit: Callable) float[source]
LOSS: Compute Fidelity between VQE PSI (output of q_circuit(params)) and TRUE PSI computed by diagonalizing the Hamiltonian
- Parameters:
Y (np.ndarray) – Array of true wavefunction obtained by diagonalizing the Hamiltonians
params (np.ndarray) – Array of parameters of the VQE circuits
q_circuit (fun) – Quantum function of the VQE circuit
- Returns:
Mean fidelities between VQE PSI and TRUE PSI
- Return type:
float
PhaseEstimation.qcnn module
This module implements the base functions to implement a Quantum Convolutional Neural Network (QCNN) for the (ANNNI) Ising Model.
- PhaseEstimation.qcnn.ANNNI_accuracy(qcnnclass: qcnn, plot: bool = False) float[source]
Compute accuracy of the QCNN of the whole ANNNI state space
- Parameters:
qcnnclass (qcnn) – QCNN class
plot (bool) –
- if True -> displays the plot of the accuracy:
if green: sample correctly classified if red : sample wrongly classified
- Returns:
Accuracy : (# samples correctly classified)/(# samples) (0,1)
- Return type:
float
- PhaseEstimation.qcnn.get_trainset_gaussian(vqeclass: vqe, nS: int, sigma: float = 1) List[int][source]
Draw randomly samples from the training for each axis according to the gaussian distribution centered around the phase transition on the axis and std sigma
- Parameters:
vqeclass (vqe.vqe) – VQE class to get the side size of the system
nS (int) – Number of samples to draw in total
sigma (float) – Standard deviation of the two distributions
- Returns:
List of the indexes of the subset of the training set
- Return type:
np.ndarray
- PhaseEstimation.qcnn.load(filename_vqe: str, filename_qcnn: str) qcnn[source]
Load QCNN from VQE file and QCNN file
- Parameters:
filename_vqe (str) – Name of the file from where to load the VQE class
filename_qcnn (str) – Name of the file from where to load the main parameters of the QCNN class
- Returns:
QCNN class
- Return type:
class
- class PhaseEstimation.qcnn.qcnn(vqe: vqe, qcnn_circuit: Callable, n_outputs: int = 1)[source]
Bases:
object- predict()[source]
Get the phases probabilities for each VQE state
- Returns:
List of probabilities
- Return type:
List[List[Number]]
- predict_lines(predictions=[])[source]
Get the prdicted phase-transition line
- Parameters:
predictions (List[List[Number]]) – This is the output of self.predict(), if it is not passed, the predictions will be computed asnew
- Returns:
y-coordinate of the transition point for each kappa value
- Return type:
List[Number]
- save(filename: str)[source]
Saves QCNN parameters to file
- Parameters:
filename (str) – File where to save the parameters
- train(lr: float, n_epochs: int, train_index: List[Number], loss_fn: Callable, circuit: bool = False, plot: bool = False)[source]
Training function for the QCNN.
- Parameters:
lr (float) – Learning rate for the ADAM optimizer
n_epochs (int) – Total number of epochs for each learning
train_index (np.ndarray) – Index of training points
loss_fn (function) – Loss function
circuit (bool) – if True -> Prints the circuit
plot (bool) – if True -> It displays loss curve
- PhaseEstimation.qcnn.qcnn_circuit(params: List[Number], N: int, n_outputs: int) Tuple[int, List[int]][source]
Building function for the QCNN circuit:
- Parameters:
params (np.ndarray) – Array of QCNN parameters
N (int) – Number of qubits
n_outputs (int) – Output vector dimension
- Returns:
int – Total number of parameters needed to build this circuit
np.ndarray – Array of indexes of not-measured wires (due to pooling)
PhaseEstimation.visualization module
Plotting functions for the classes hamiltonians, vqe, qcnn, encoder. This functions are not meant to be used directly, but are called within their respective classes
- PhaseEstimation.visualization.ENC_show_compression_ANNNI(encclass, trainingpoint=False, label=False, plot3d=False)[source]
Plots performance of the compression on the whole data for an encoder on the ANNI model
- Parameters:
encoder (encoder.encoder) – Custom encoder class after being trained
trainingpoint (int) – Mark the single training point on the plot
label (str) – Label to assign to the picture, needed for the paper
plot3d (bool) – If True the 3D plot will be displayed aswell
- PhaseEstimation.visualization.HAM_mass_gap(Hs, phase_lines=False, pe_line=False)[source]
Shows the mass gap which is defined as the difference between the first excited leven and the ground energy level for each point in the parameter space.
- Parameters:
Hs (hamiltonians.hamiltonian) – Custom hamiltonian class, it is needed to call plot_layout
phase_lines (bool) – if True plots the phase transition lines
pe_line (bool) – if True plots Peshel Emery line
- PhaseEstimation.visualization.HAM_phases_plot(Hs)[source]
Shows the division of phases of the parameter space according to the state-of-the-art lines
- Parameters:
Hs (hamiltonians.hamiltonian) – Custom hamiltonian class, it is needed to call plot_layout
- PhaseEstimation.visualization.QCNN_classification_ANNNI(qcnnclass, hard_thr=True, predicted_line=False, label=False, info=False)[source]
Plots performance of the classifier on the whole data for a QCNN of a ANNI model
- Parameters:
qcnnclass (qcnn.qcnn) – Custom QCNN class after being trained
hard_thr (bool) – if True the prediction will be displayed through an argmax instead of using color channels to entail the 3 (4 considering the trash case) probabilities
predicted_line (bool) – if True it displays the predicted transition line
label (str) – Label to assign to the picture, needed for the paper
info (bool) – if True more infos will be displayed such as the names of the phases
- PhaseEstimation.visualization.QCNN_classification_ANNNI_marginal(qcnnclass)[source]
Displays the probabilities of the states on the two axes. It is used more as a debug function to test if the classes are being trained correctly.
- Parameters:
qcnnclass (qcnn.qcnn) – Custom QCNN class after being trained
- PhaseEstimation.visualization.QCNN_classification_ising(qcnnclass, train_index)[source]
Plots performance of the classifier on the whole data for a QCNN of a Nearest Neighbour Interaction Hamiltonian
- Parameters:
qcnnclass (qcnn.qcnn) – Custom QCNN class after being trained
train_index (List[Number]) – List of the indexes of the training set. On displaying they will be marked with a different colour
- PhaseEstimation.visualization.VQE_fidelity_slice(vqeclass, slice_value, axis=0, truestates=False)[source]
Shows confusion matrix of fidelities of only a ‘slice’ of states in the parameter space. In other words, it will be computed the fidelity of each state among every other that share the same h or kappa.
- Parameters:
vqeclass (vqe.vqe) – Custom VQE class after being trained
slice_value (float) – if axis = 0, then we will pick only the states having h = slice_value and kappa whatever if axis = 1, then we will pick only the states having kappa = slice_value and h whatever
axis (int) – Direction of where to slide, 0 is horizontal (fixed h), 1 is vertical (fixed kappa)
truestates (bool) – if True the true states will be employed if False the VQE states will be employed
- PhaseEstimation.visualization.VQE_psi_truepsi_fidelity(vqeclass, phase_lines=False, pe_line=False)[source]
For each VQE resulting state, show its fidelity compared to its true state obtained through diagonalization of the Hamiltonian:
- Parameters:
vqeclass (vqe.vqe) – Custom VQE class after being trained
phase_lines (bool) – if True plots the phase transition lines
pe_line (bool) – if True plots Peshel Emery line
- PhaseEstimation.visualization.VQE_show_annni(vqeclass, log_heatmap=False, plot3d=True, phase_lines=False, pe_line=False)[source]
Shows results of a trained VQE (ANNNI) run:
- Parameters:
vqeclass (vqe.vqe) – Custom VQE class after being trained
log_heatmap (bool) – if True, the accuracy is displayed in logscale
plot3d (bool) – if True the predicted energies and true energies will be displayed in a 3D plot
phase_lines (bool) – if True plots the phase transition lines
pe_line (bool) – if True plots Peshel Emery line
- PhaseEstimation.visualization.VQE_show_isingchain(vqeclass)[source]
Shows results of a trained VQE (Nearest Neighbour Ising Model) run
- Parameters:
vqeclass (vqe.vqe) – Custom VQE class after being trained
- PhaseEstimation.visualization.getlines_from_Hs(Hs, func: Callable, xrange: List[float], res: int = 100, **kwargs)[source]
Plot function func from xrange[0] to xrange[1] This function uses the Hamiltonians class to plot the function according to the ranges of its parameters
- Parameters:
Hs (hamiltonians.hamiltonian) – Custom Hamiltonian class
func (function) – Function to plot, usually: > general.paraanti : Transition line between paramagnetic phase and antiphase; > general.paraferro : Transition line between paramagnetic phase and ferromagnetic phase; > general.b1 : Pseudo-transition line inside the antiphase subspace; > general.peshel_emery :Peshel Emery Line.
- PhaseEstimation.visualization.plot_layout(Hs, pe_line, phase_lines, title, figure_already_defined=False)[source]
Many plotting functions here have the same layout, this function will be called inside the others to have a standard layout
- Parameters:
Hs (hamiltonians.hamiltonian) – Custom hamiltonian class, it is needed to set xlim and ylim and ticks
pe_line (bool) – if True plots Peshel Emery line
phase_lines (bool) – if True plots the phase transition lines
title (str) – Title of the legent of the plot
figure_already_defined (bool) – if False it calls the plt.figure function
PhaseEstimation.vqe module
This module implements the base function to implement a VQE.
- PhaseEstimation.vqe.circuit_ising(N: int, params: List[Number]) int[source]
Full VQE circuit Number of parameters (gates): 7*N
- Parameters:
N (int) – Number of qubits
params (np.ndarray) – Array of parameters/rotation for the circuit
- Returns:
Total number of parameters needed to build this circuit
- Return type:
int
- PhaseEstimation.vqe.circuit_ising2(N: int, params: List[Number]) int[source]
Full VQE circuit, enhanced version of circuit_ising, higher number of parameters Number of parameters (gates): 11*N
- Parameters:
N (int) – Number of qubits
params (np.ndarray) – Array of parameters/rotation for the circuit
- Returns:
Total number of parameters needed to build this circuit
- Return type:
int
- PhaseEstimation.vqe.circuit_ising3(N: int, params: List[Number]) int[source]
Shorter and more real circuit
- Parameters:
N (int) – Number of qubits
params (np.ndarray) – Array of parameters/rotation for the circuit
- Returns:
Total number of parameters needed to build this circuit
- Return type:
int
- PhaseEstimation.vqe.load_vqe(filename: str) vqe[source]
Load main parameters of a VQE class saved to a local file using vqe.save(filename)
- Parameters:
filename (str) – Local file from where to load the parameters
- Returns:
VQE class with main parameters
- Return type:
class
- class PhaseEstimation.vqe.vqe(Hs: hamiltonian, circuit: Callable)[source]
Bases:
object- save(filename: str)[source]
Save main parameters of the VQE class to a local file. Parameters saved: > Hs class, vqe parameters, circuit function
- Parameters:
filename (str) – Local file to save the parameters
- show(**kwargs)[source]
Shows results of a trained VQE (ANNNI) run:
- Parameters:
log_heatmap (bool) – (IF ANNNI) if True, the accuracy is displayed in logscale
plot3d (bool) – (IF ANNNI) if True the predicted energies and true energies will be displayed in a 3D plot
phase_lines (bool) – (IF ANNNI) if True plots the phase transition lines
pe_line (bool) – (IF ANNNI) if True plots Peshel Emery line
- show_fidelity(**kwargs)[source]
For each VQE resulting state, show its fidelity compared to its true state obtained through diagonalization of the Hamiltonian:
- Parameters:
phase_lines (bool) – if True plots the phase transition lines
pe_line (bool) – if True plots Peshel Emery line
- show_fidelity_slice(slice_value, axis=0, truestates=False)[source]
Shows confusion matrix of fidelities of only a ‘slice’ of states in the parameter space. In other words, it will be computed the fidelity of each state among every other that share the same h or kappa.
- Parameters:
slice_value (float) – if axis = 0, then we will pick only the states having h = slice_value and kappa whatever if axis = 1, then we will pick only the states having kappa = slice_value and h whatever
axis (int) – Direction of where to slide, 0 is horizontal (fixed h), 1 is vertical (fixed kappa)
truestates (bool) – if True the true states will be employed if False the VQE states will be employed
- train(lr: Number, n_epochs: int, circuit: bool = False)[source]
Training function for the VQE.
- Parameters:
lr (float) – Learning rate to be multiplied in the circuit-gradient output
n_epochs (int) – Total number of epochs for each learning
circuit (bool) – if True -> Prints the circuit
- train_refine(lr: Number, n_epochs: int, acc_thr: Number, assist: bool = False)[source]
Training only the sites that have an accuracy score worse (higher) than acc_thr
- Parameters:
lr (float) – Learning rate to be multiplied in the circuit-gradient output
n_epochs (int) – Total number of epochs for each learning
acc_thr (float) – Accuracy threshold for which selecting the sites to train
assist (bool) – if True -> Each site that will be trained will start from the neighbouring site that has the better accuracy