Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# Get scaled density-density terms and basis transformation matrices.
self.scaled_density_density_matrices = [] # type: List[numpy.ndarray]
self.basis_change_matrices = [] # type: List[numpy.ndarray]
for j in range(len(self.eigenvalues)):
density_density_matrix, basis_change_matrix = (
openfermion.prepare_one_body_squared_evolution(
one_body_squares[j]))
self.scaled_density_density_matrices.append(
numpy.real(self.eigenvalues[j] * density_density_matrix))
self.basis_change_matrices.append(basis_change_matrix)
# Get transformation matrix and orbital energies for one-body terms
one_body_coefficients = (
hamiltonian.one_body_tensor + self.one_body_correction)
quad_ham = openfermion.QuadraticHamiltonian(one_body_coefficients)
self.one_body_energies, self.one_body_basis_change_matrix, _ = (
quad_ham.diagonalizing_bogoliubov_transform()
)
super().__init__(qubits)
# Get scaled density-density terms and basis transformation matrices.
self.scaled_density_density_matrices = [] # type: List[numpy.ndarray]
self.basis_change_matrices = [] # type: List[numpy.ndarray]
for j in range(len(self.eigenvalues)):
density_density_matrix, basis_change_matrix = (
openfermion.prepare_one_body_squared_evolution(
self.one_body_squares[j]))
self.scaled_density_density_matrices.append(
numpy.real(self.eigenvalues[j] * density_density_matrix))
self.basis_change_matrices.append(basis_change_matrix)
# Get transformation matrix and orbital energies for one-body terms
one_body_coefficients = (
hamiltonian.one_body_tensor + one_body_correction)
quad_ham = openfermion.QuadraticHamiltonian(one_body_coefficients)
self.one_body_energies, self.one_body_basis_change_matrix, _ = (
quad_ham.diagonalizing_bogoliubov_transform()
)
super().__init__(hamiltonian)
def second_order_trotter_step(
self,
qubits: Sequence[QubitId],
hamiltonian: DiagonalCoulombHamiltonian,
time: float,
control_qubit: Optional[QubitId]=None
) -> cirq.OP_TREE:
n_qubits = len(qubits)
quad_ham = QuadraticHamiltonian(hamiltonian.one_body)
# Get the coefficients of the one-body terms in the diagonalizing basis
orbital_energies, _ = quad_ham.orbital_energies()
# Get the Bogoliubov transformation matrix that diagonalizes the
# one-body term
transformation_matrix = quad_ham.diagonalizing_bogoliubov_transform()
# Simulate the one-body terms for half of the full time
yield (cirq.CZ(control_qubit, qubits[i])**(
-orbital_energies[i] * 0.5 * time / numpy.pi)
for i in range(n_qubits))
# Rotate to the computational basis
yield bogoliubov_transform(qubits, transformation_matrix)
# Simulate the two-body terms for the full time
def __init__(self, hamiltonian: DiagonalCoulombHamiltonian) -> None:
quad_ham = QuadraticHamiltonian(hamiltonian.one_body)
# Get the basis change matrix that diagonalizes the one-body term
# and associated orbital energies
self.orbital_energies, self.basis_change_matrix, _ = (
quad_ham.diagonalizing_bogoliubov_transform()
)
super().__init__(hamiltonian)
params = []
for param in self.params():
i = param.subscripts[-1]
# Use the midpoint of the time segment
interpolation_progress = 0.5 * (2 * i + 1) / self.iterations
# One-body term
if param.letter == 'U' and len(param.subscripts) == 2:
p, _ = param.subscripts
one_body_coefficients = (
self.hamiltonian.one_body_tensor
+ interpolation_progress * self.one_body_correction)
quad_ham = openfermion.QuadraticHamiltonian(
one_body_coefficients)
one_body_energies, _, _ = (
quad_ham.diagonalizing_bogoliubov_transform())
params.append(_canonicalize_exponent(
-one_body_energies[p]
* step_time / numpy.pi, 2))
# Off-diagonal one-body term
elif param.letter == 'V':
p, q, j, _ = param.subscripts
two_body_coefficients = (
self.scaled_density_density_matrices[j])
params.append(_canonicalize_exponent(
-2 * two_body_coefficients[p, q]
* interpolation_progress
* step_time / numpy.pi, 2))
of the entries of the two-body tensor of the Hamiltonian.
qubits: Qubits to be used by the ansatz circuit. If not specified,
then qubits will automatically be generated by the
`_generate_qubits` method.
"""
self.hamiltonian = hamiltonian
self.iterations = iterations
self.include_all_cz = include_all_cz
self.include_all_z = include_all_z
if adiabatic_evolution_time is None:
adiabatic_evolution_time = (
numpy.sum(numpy.abs(hamiltonian.two_body)))
self.adiabatic_evolution_time = cast(float, adiabatic_evolution_time)
quad_ham = openfermion.QuadraticHamiltonian(hamiltonian.one_body)
# Get the basis change matrix that diagonalizes the one-body term
# and associated orbital energies
self.orbital_energies, self.basis_change_matrix, _ = (
quad_ham.diagonalizing_bogoliubov_transform()
)
super().__init__(qubits)
def finish(self,
qubits: Sequence[QubitId],
hamiltonian: DiagonalCoulombHamiltonian,
n_steps: int,
control_qubit: Optional[QubitId]=None
) -> cirq.OP_TREE:
# Rotate back to the computational basis
quad_ham = QuadraticHamiltonian(hamiltonian.one_body)
yield bogoliubov_transform(
qubits, quad_ham.diagonalizing_bogoliubov_transform())
# If the number of Trotter steps is odd, swap qubits back
if n_steps & 1:
yield swap_network(qubits)