Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions runtime/cudaq/dynamics/spin_operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,16 +183,16 @@ sum_op<spin_handler> spin_handler::minus(int degree) {
}

namespace spin {
sum_op<spin_handler> i(std::size_t target) {
product_op<spin_handler> i(std::size_t target) {
return spin_handler::i(target);
}
sum_op<spin_handler> x(std::size_t target) {
product_op<spin_handler> x(std::size_t target) {
return spin_handler::x(target);
}
sum_op<spin_handler> y(std::size_t target) {
product_op<spin_handler> y(std::size_t target) {
return spin_handler::y(target);
}
sum_op<spin_handler> z(std::size_t target) {
product_op<spin_handler> z(std::size_t target) {
return spin_handler::z(target);
}
}
Expand Down
12 changes: 7 additions & 5 deletions runtime/cudaq/operators.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,14 @@ class sum_op {
std::vector<std::vector<HandlerTy>> terms;
std::vector<scalar_operator> coefficients;

constexpr sum_op(){};
sum_op(const sum_op<HandlerTy> &other, bool sized, int size);
sum_op(sum_op<HandlerTy> &&other, bool sized, int size);

public:

// Default constructor constructs an identity term.
constexpr sum_op() : sum_op(1.0) {}

// called const_iterator because it will *not* modify the sum,
// regardless of what is done with the products/iterator
struct const_iterator {
Expand Down Expand Up @@ -931,10 +933,10 @@ extern template class sum_op<fermion_handler>;

// Here only for backward compatibility
namespace spin {
sum_op<spin_handler> i(std::size_t target);
sum_op<spin_handler> x(std::size_t target);
sum_op<spin_handler> y(std::size_t target);
sum_op<spin_handler> z(std::size_t target);
product_op<spin_handler> i(std::size_t target);
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These changes allow you to create spin_op_term's from i,x,y,z.

Copy link
Copy Markdown
Owner

@bettinaheim bettinaheim Mar 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is what I had originally, but if you want backward compatibility, it's a sum here. The product version that should be used is spin_op::x

Copy link
Copy Markdown
Author

@bmhowe23 bmhowe23 Mar 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A product_op is implicitly convertible to a sum_op, but not the other way around, so this change allows for the most flexibility in my opinion. (You are correct that what you had is the most backwards compatible by forcing it to return spin_op, but during the course of the 2704 PR, I had converted some things to spin_op_term (i.e. product_op), and my suggested change here allows seamless usage in both cases, I believe.)

Maybe I should've said: this allows for a good mix of backward compatibility and forward compatibility.

product_op<spin_handler> x(std::size_t target);
product_op<spin_handler> y(std::size_t target);
product_op<spin_handler> z(std::size_t target);
}

} // namespace cudaq
11 changes: 11 additions & 0 deletions runtime/cudaq/spin_op.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
/****************************************************************-*- C++ -*-****
* 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. *
******************************************************************************/

#pragma once
Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding this file just means people don't have to change their #include's. After all, they are still dealing with the spin_op data type.


#include "cudaq/operators.h"
Loading