Skip to content

Commit

Permalink
Remove parsing catalyst device attribute dictionary for shots (#1017)
Browse files Browse the repository at this point in the history
**Context:**
As part of the work to support dynamic measurement shapes,
PennyLaneAI/catalyst#1310 in Catalyst is
allowing a quantum device to take in a dynamic number of shots at the
mlir level. One of the changes involved is that the `DeviceInitOp` now
takes in a proper SSA argument for shots, instead of having shots as
just another entry in the `DeviceInitOp`'s attribute dictionary.

Correspondingly, the backend devices' catalyst interfaces need to stop
parsing the `DeviceInitOp`'s attribute dictionary for shots.

**Description of the Change:**
The backend devices' catalyst interfaces stop parsing the
`DeviceInitOp`'s attribute dictionary for shots.

**Benefits:**
Agreement with Catalyst on how device shots, potentially dynamic, is
handled.

**Possible Drawbacks:**

**Related GitHub Issues:**

---------

Co-authored-by: ringo-but-quantum <[email protected]>
  • Loading branch information
paul0403 and ringo-but-quantum authored Dec 5, 2024
1 parent ded6ef9 commit 17bf594
Show file tree
Hide file tree
Showing 6 changed files with 11 additions and 18 deletions.
3 changes: 3 additions & 0 deletions .github/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@

### Improvements

* Catalyst device interfaces support dynamic shots, and no longer parses the device init op's attribute dictionary for a static shots literal.
[(#1017)](https://github.com/PennyLaneAI/pennylane-lightning/pull/1017)

* Reduce flaky test and increase test shots count.
[(#1015)](https://github.com/PennyLaneAI/pennylane-lightning/pull/1015)

Expand Down
2 changes: 1 addition & 1 deletion pennylane_lightning/core/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,4 @@
Version number (major.minor.patch[-label])
"""

__version__ = "0.40.0-dev29"
__version__ = "0.40.0-dev30"
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ class LightningGPUSimulator final : public Catalyst::Runtime::QuantumDevice {
Catalyst::Runtime::CacheManager<std::complex<double>> cache_manager{};
bool tape_recording{false};

std::size_t device_shots;
std::size_t device_shots{0};

std::mt19937 *gen{nullptr};

Expand Down Expand Up @@ -108,9 +108,6 @@ class LightningGPUSimulator final : public Catalyst::Runtime::QuantumDevice {
public:
explicit LightningGPUSimulator(const std::string &kwargs = "{}") {
auto &&args = Catalyst::Runtime::parse_kwargs(kwargs);
device_shots = args.contains("shots")
? static_cast<std::size_t>(std::stoll(args["shots"]))
: 0;
}
~LightningGPUSimulator() = default;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,6 @@ class LightningKokkosSimulator final : public Catalyst::Runtime::QuantumDevice {
explicit LightningKokkosSimulator(
const std::string &kwargs = "{}") noexcept {
auto &&args = Catalyst::Runtime::parse_kwargs(kwargs);
device_shots = args.contains("shots")
? static_cast<std::size_t>(std::stoll(args["shots"]))
: 0;
}
~LightningKokkosSimulator() noexcept = default;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class LightningSimulator final : public Catalyst::Runtime::QuantumDevice {
Catalyst::Runtime::QubitManager<QubitIdType, size_t> qubit_manager{};
Catalyst::Runtime::CacheManager<std::complex<double>> cache_manager{};
bool tape_recording{false};
size_t device_shots;
size_t device_shots{0};

std::mt19937 *gen = nullptr;

Expand Down Expand Up @@ -101,9 +101,6 @@ class LightningSimulator final : public Catalyst::Runtime::QuantumDevice {
const std::string &kwargs = "{}") // NOLINT(hicpp-member-init)
{
auto &&args = Catalyst::Runtime::parse_kwargs(kwargs);
device_shots = args.contains("shots")
? static_cast<size_t>(std::stoll(args["shots"]))
: 0;
mcmc = args.contains("mcmc") ? args["mcmc"] == "True" : false;
num_burnin = args.contains("num_burnin")
? static_cast<size_t>(std::stoll(args["num_burnin"]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@ TEST_CASE("Test parse_kwargs coverage", "[Utils]") {
std::string case1;
CHECK(parse_kwargs(case1).empty());

std::string case2{"{shots : 1000}"};
std::string case3{"shots : 1000"};
std::string case4{"'shots':'1000'"};
std::string case2{"{my_attr : 1000}"};
std::string case3{"my_attr : 1000"};
std::string case4{"'my_attr':'1000'"};
CHECK(parse_kwargs(case2) == parse_kwargs(case3));
CHECK(parse_kwargs(case3) == parse_kwargs(case4));

Expand All @@ -40,13 +40,12 @@ TEST_CASE("Test parse_kwargs coverage", "[Utils]") {
CHECK((res5.contains("E") && res5["E"] == "F"));

std::string case6{
"device_type : braket.aws.qubit,{'shots': 0, 'device_arn': 'sv1', "
"device_type : braket.aws.qubit,{'device_arn': 'sv1', "
"'s3_destination_folder': \"('catalyst-op3-s3', 'prefix')\"}"};
auto res6 = parse_kwargs(case6);
CHECK(res6.size() == 4);
CHECK(res6.size() == 3);
CHECK((res6.contains("device_type") &&
res6["device_type"] == "braket.aws.qubit"));
CHECK((res6.contains("shots") && res6["shots"] == "0"));
CHECK((res6.contains("device_arn") && res6["device_arn"] == "sv1"));
CHECK((res6.contains("s3_destination_folder") &&
res6["s3_destination_folder"] == "('catalyst-op3-s3', 'prefix')"));
Expand Down

0 comments on commit 17bf594

Please sign in to comment.