Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def uccsd_singlet_evolution(packed_amplitudes, n_qubits, n_electrons,
fermion_transform=jordan_wigner):
"""Create a ProjectQ evolution operator for a UCCSD singlet circuit
Args:
packed_amplitudes(ndarray): Compact array storing the unique single
and double excitation amplitudes for a singlet UCCSD operator.
The ordering lists unique single excitations before double
excitations.
n_qubits(int): Number of spin-orbitals used to represent the system,
which also corresponds to number of qubits in a non-compact map.
n_electrons(int): Number of electrons in the physical system
fermion_transform(openfermion.transform): The transformation that
defines the mapping from Fermions to QubitOperator.
Returns:
evoution_operator(TimeEvolution): The unitary operator
that constructs the UCCSD singlet state.
def generate(self, inputParams):
geom = ast.literal_eval(inputParams['geometry']) if isinstance(inputParams['geometry'],str) else inputParams['geometry']
mdata = MolecularData(geom,
inputParams['basis'],
int(inputParams['multiplicity']),
int(inputParams['charge']))
molecule = run_psi4(mdata, run_scf=1, run_fci=1)
from openfermion.transforms import get_sparse_operator, jordan_wigner
from openfermion.utils import sparse_eigenspectrum
from scipy.sparse import find
from scipy.optimize import minimize
fermiOp = get_fermion_operator(molecule.get_molecular_hamiltonian())
qop = jordan_wigner(fermiOp)
inputParams['fci'] = molecule.fci_energy
inputParams['hf'] = molecule.hf_energy
return xaccvqe.QubitOperator2XACC(qop)
# Construct the auxiliary Hamiltonian
aux_ham = FermionOperator()
for i, j in aux_ham_graph.edges():
aux_ham -= stabilizer_local_2d_square(
i, j, x_dimension, y_dimension)
# Add an identity term to ensure that the auxiliary Hamiltonian
# has ground energy equal to zero
aux_ham += FermionOperator((), aux_ham_graph.size())
# Scale the auxiliary Hamiltonian
aux_ham *= aux_ham_coefficient
# Add it to the operator
transformed_operator += jordan_wigner(aux_ham)
return transformed_operator
Measure the alpha-alpha block of the 2-RDM
:param spatial_dim: size of spatial basis function
:param variance_bound: variance bound for measurement. Right now this is
the bound on the variance if you summed up all the
individual terms. 1.0E-6 is a good place to start.
:param program: a pyquil Program
:param quantum_resource: a quantum abstract machine connection object
:param transform: fermion-to-qubit transform
:param label_map: qubit label re-mapping if different physical qubits are
desired
:return: the alpha-alpha block of the 2-RDM
"""
# first get the pauli terms corresponding to the alpha-alpha block
pauli_terms_in_aa = pauli_terms_for_tpdm_aa(spatial_dim,
transform=jordan_wigner)
if label_map is not None:
pauli_terms_in_aa = pauli_term_relabel(sum(pauli_terms_in_aa),
label_map)
rev_label_map = dict(zip(label_map.values(), label_map.keys()))
result_dictionary = _measure_list_of_pauli_terms(pauli_terms_in_aa,
variance_bound,
program,
quantum_resource)
if label_map is not None:
result_dictionary = pauli_dict_relabel(result_dictionary, rev_label_map)
d2aa = pauli_to_tpdm_aa(spatial_dim, result_dictionary, transform=transform)
return d2aa
col, _ = snake_index_to_coordinates(
top, x_dimension, y_dimension)
# Multiply by a stabilizer. If the column is even, the
# stabilizer corresponds to an edge that points down;
# otherwise, the edge points up
if col % 2 == 0:
transformed_term *= stabilizer(top_aux, bot_aux)
else:
transformed_term *= stabilizer(bot_aux, top_aux)
# Update the auxiliary Hamiltonian coefficient
aux_ham_coefficient += abs(coefficient)
# Transform the term to a QubitOperator and add it to the operator
transformed_operator += jordan_wigner(transformed_term)
# Add the auxiliary Hamiltonian if requested and compute the
# resulting energy shift
if add_auxiliary_hamiltonian:
# Construct the auxiliary Hamiltonian graph
aux_ham_graph = auxiliary_graph_2d_square(x_dimension, y_dimension)
# Construct the auxiliary Hamiltonian
aux_ham = FermionOperator()
for i, j in aux_ham_graph.edges():
aux_ham -= stabilizer_local_2d_square(
i, j, x_dimension, y_dimension)
# Add an identity term to ensure that the auxiliary Hamiltonian
# has ground energy equal to zero
aux_ham += FermionOperator((), aux_ham_graph.size())
# generating the fermionic Hamiltonian
fermionic_hamiltonian = get_fermion_operator(terms_molecular_hamiltonian)
mapping = mapping.strip().lower()
if mapping not in ("jordan_wigner", "bravyi_kitaev"):
raise TypeError(
"The '{}' transformation is not available. \n "
"Please set 'mapping' to 'jordan_wigner' or 'bravyi_kitaev'.".format(mapping)
)
# fermionic-to-qubit transformation of the Hamiltonian
if mapping == "bravyi_kitaev":
return bravyi_kitaev(fermionic_hamiltonian)
return jordan_wigner(fermionic_hamiltonian)
def fswap(register, mode_a, mode_b, fermion_to_spin_mapping=jordan_wigner):
"""Apply the fermionic swap operator to two modes.
The fermionic swap is applied to the qubits corresponding to mode_a
and mode_b, after the Jordan-Wigner transformation.
Args:
register (projectq.Qureg): The register of qubits to act on.
mode_a, mode_b (int): The two modes to swap.
fermion_to_spin_mapping (function): Transformation from fermionic
to spin operators to use. Defaults to jordan_wigner.
"""
operator = fswap_generator(mode_a, mode_b)
TimeEvolution(numpy.pi / 2., fermion_to_spin_mapping(operator)) | register
def get_interaction_rdm(qubit_operator, n_qubits=None):
"""Build an InteractionRDM from measured qubit operators.
Returns: An InteractionRDM object.
"""
# Avoid circular import.
from openfermion.transforms import jordan_wigner
if n_qubits is None:
n_qubits = count_qubits(qubit_operator)
one_rdm = numpy.zeros((n_qubits,) * 2, dtype=complex)
two_rdm = numpy.zeros((n_qubits,) * 4, dtype=complex)
# One-RDM.
for i, j in itertools.product(range(n_qubits), repeat=2):
transformed_operator = jordan_wigner(FermionOperator(((i, 1), (j, 0))))
for term, coefficient in iteritems(transformed_operator.terms):
if term in qubit_operator.terms:
one_rdm[i, j] += coefficient * qubit_operator.terms[term]
# Two-RDM.
for i, j, k, l in itertools.product(range(n_qubits), repeat=4):
transformed_operator = jordan_wigner(FermionOperator(((i, 1), (j, 1),
(k, 0), (l, 0))))
for term, coefficient in iteritems(transformed_operator.terms):
if term in qubit_operator.terms:
two_rdm[i, j, k, l] += coefficient * qubit_operator.terms[term]
return InteractionRDM(one_rdm, two_rdm)
def fourier_transform_0(register, mode_a, mode_b):
"""Apply the fermionic Fourier transform to two modes.
The fermionic Fourier transform is applied to the qubits
corresponding to mode_a and mode_b, , after the Jordan-Wigner
Args:
register (projectq.Qureg): The register of qubits to act on.
mode_a, mode_b (int): The two modes to Fourier transform.
"""
operator = fourier_transform_0_generator(mode_a, mode_b)
jw_operator = jordan_wigner(operator)
Z | register[mode_b]
TimeEvolution(numpy.pi / 8., jw_operator) | register