-
Notifications
You must be signed in to change notification settings - Fork 204
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Adding Rydberg hamiltonian operator
* Adding unitests for Rydberg hamiltonian operator * Making evaluate function in scalar_operator const Signed-off-by: Sachin Pisal <spisal@nvidia.com>
- Loading branch information
Showing
6 changed files
with
232 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. * | ||
* All rights reserved. * | ||
* * | ||
* This source code and the accompanying materials are made available under * | ||
* the terms of the Apache License 2.0 which accompanies this distribution. * | ||
******************************************************************************/ | ||
|
||
#include "cudaq/operators.h" | ||
#include <sstream> | ||
#include <stdexcept> | ||
|
||
namespace cudaq { | ||
rydberg_hamiltonian::rydberg_hamiltonian( | ||
const std::vector<Coordinate> &atom_sites, const scalar_operator &litude, | ||
const scalar_operator &phase, const scalar_operator &delta_global, | ||
const std::vector<int> &atom_filling, | ||
const std::optional<std::pair<scalar_operator, std::vector<double>>> | ||
&delta_local) | ||
: atom_sites(atom_sites), amplitude(amplitude), phase(phase), | ||
delta_global(delta_global), delta_local(delta_local) { | ||
if (atom_filling.empty()) { | ||
this->atom_filling = std::vector<int>(atom_sites.size(), 1); | ||
} else if (atom_sites.size() != atom_filling.size()) { | ||
throw std::invalid_argument( | ||
"Size of `atom_sites` and `atom_filling` must be equal."); | ||
} else { | ||
this->atom_filling = atom_filling; | ||
} | ||
|
||
if (delta_local.has_value()) { | ||
throw std::runtime_error( | ||
"Local detuning is an experimental feature not yet supported."); | ||
} | ||
} | ||
|
||
const std::vector<rydberg_hamiltonian::Coordinate> & | ||
rydberg_hamiltonian::get_atom_sites() const { | ||
return atom_sites; | ||
} | ||
|
||
const std::vector<int> &rydberg_hamiltonian::get_atom_filling() const { | ||
return atom_filling; | ||
} | ||
|
||
const scalar_operator &rydberg_hamiltonian::get_amplitude() const { | ||
return amplitude; | ||
} | ||
|
||
const scalar_operator &rydberg_hamiltonian::get_phase() const { return phase; } | ||
|
||
const scalar_operator &rydberg_hamiltonian::get_delta_global() const { | ||
return delta_global; | ||
} | ||
} // namespace cudaq |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
/******************************************************************************* | ||
* Copyright (c) 2022 - 2025 NVIDIA Corporation & Affiliates. * | ||
* All rights reserved. * | ||
* * | ||
* This source code and the accompanying materials are made available under * | ||
* the terms of the Apache License 2.0 which accompanies this distribution. * | ||
******************************************************************************/ | ||
|
||
#include "cudaq/operators.h" | ||
#include <gtest/gtest.h> | ||
|
||
using namespace cudaq; | ||
|
||
TEST(RydbergHamiltonianTest, ConstructorValidInputs) { | ||
// Valid atom sites | ||
std::vector<rydberg_hamiltonian::Coordinate> atom_sites = { | ||
{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}, {1.0, 1.0}}; | ||
|
||
// Valid operators | ||
scalar_operator amplitude(1.0); | ||
scalar_operator phase(0.0); | ||
scalar_operator delta_global(-0.5); | ||
|
||
// Valid atom filling | ||
rydberg_hamiltonian hamiltonian(atom_sites, amplitude, phase, delta_global); | ||
|
||
EXPECT_EQ(hamiltonian.get_atom_sites().size(), atom_sites.size()); | ||
EXPECT_EQ(hamiltonian.get_atom_filling().size(), atom_sites.size()); | ||
EXPECT_EQ(hamiltonian.get_amplitude().evaluate({}), | ||
std::complex<double>(1.0, 0.0)); | ||
EXPECT_EQ(hamiltonian.get_phase().evaluate({}), | ||
std::complex<double>(0.0, 0.0)); | ||
EXPECT_EQ(hamiltonian.get_delta_global().evaluate({}), | ||
std::complex<double>(-0.5, 0.0)); | ||
} | ||
|
||
TEST(RydbergHamiltonianTest, ConstructorWithAtomFilling) { | ||
std::vector<rydberg_hamiltonian::Coordinate> atom_sites = { | ||
{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}}; | ||
|
||
// Valid operators | ||
scalar_operator amplitude(1.0); | ||
scalar_operator phase(0.0); | ||
scalar_operator delta_global(-0.5); | ||
|
||
// Valid atom filling | ||
std::vector<int> atom_filling = {1, 0, 1}; | ||
|
||
rydberg_hamiltonian hamiltonian(atom_sites, amplitude, phase, delta_global, | ||
atom_filling); | ||
|
||
EXPECT_EQ(hamiltonian.get_atom_sites().size(), atom_sites.size()); | ||
EXPECT_EQ(hamiltonian.get_atom_filling(), atom_filling); | ||
} | ||
|
||
TEST(RydbergHamiltonianTest, InvalidAtomFillingSize) { | ||
std::vector<rydberg_hamiltonian::Coordinate> atom_sites = { | ||
{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}}; | ||
|
||
// Valid operators | ||
scalar_operator amplitude(1.0); | ||
scalar_operator phase(0.0); | ||
scalar_operator delta_global(-0.5); | ||
|
||
// Invalid atom filling size | ||
std::vector<int> atom_filling = {1, 0}; | ||
|
||
EXPECT_THROW(rydberg_hamiltonian(atom_sites, amplitude, phase, delta_global, | ||
atom_filling), | ||
std::invalid_argument); | ||
} | ||
|
||
TEST(RydbergHamiltonianTest, UnsupportedLocalDetuning) { | ||
std::vector<rydberg_hamiltonian::Coordinate> atom_sites = { | ||
{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}}; | ||
|
||
// Valid operators | ||
scalar_operator amplitude(1.0); | ||
scalar_operator phase(0.0); | ||
scalar_operator delta_global(-0.5); | ||
|
||
// Invalid delta_local | ||
auto delta_local = | ||
std::make_pair(scalar_operator(0.5), std::vector<double>{0.1, 0.2, 0.3}); | ||
|
||
EXPECT_THROW(rydberg_hamiltonian(atom_sites, amplitude, phase, delta_global, | ||
{}, delta_local), | ||
std::runtime_error); | ||
} | ||
|
||
TEST(RydbergHamiltonianTest, Accessors) { | ||
std::vector<rydberg_hamiltonian::Coordinate> atom_sites = { | ||
{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}}; | ||
|
||
// Valid operators | ||
scalar_operator amplitude(1.0); | ||
scalar_operator phase(0.0); | ||
scalar_operator delta_global(-0.5); | ||
|
||
rydberg_hamiltonian hamiltonian(atom_sites, amplitude, phase, delta_global); | ||
|
||
EXPECT_EQ(hamiltonian.get_atom_sites(), atom_sites); | ||
EXPECT_EQ(hamiltonian.get_amplitude().evaluate({}), | ||
std::complex<double>(1.0, 0.0)); | ||
EXPECT_EQ(hamiltonian.get_phase().evaluate({}), | ||
std::complex<double>(0.0, 0.0)); | ||
EXPECT_EQ(hamiltonian.get_delta_global().evaluate({}), | ||
std::complex<double>(-0.5, 0.0)); | ||
} | ||
|
||
TEST(RydbergHamiltonianTest, DefaultAtomFilling) { | ||
std::vector<rydberg_hamiltonian::Coordinate> atom_sites = { | ||
{0.0, 0.0}, {1.0, 0.0}, {0.0, 1.0}, {1.0, 1.0}}; | ||
|
||
// Valid operators | ||
scalar_operator amplitude(1.0); | ||
scalar_operator phase(0.0); | ||
scalar_operator delta_global(-0.5); | ||
|
||
rydberg_hamiltonian hamiltonian(atom_sites, amplitude, phase, delta_global); | ||
|
||
std::vector<int> expected_filling(atom_sites.size(), 1); | ||
EXPECT_EQ(hamiltonian.get_atom_filling(), expected_filling); | ||
} |