Skip to content

Fix broadcasting behavior and log a warning for behavior change - #898

Merged
bettinaheim merged 2 commits into
NVIDIA:releases/v0.5.0from
schweitzpgi:ch-broadcast.error
Nov 14, 2023
Merged

Fix broadcasting behavior and log a warning for behavior change#898
bettinaheim merged 2 commits into
NVIDIA:releases/v0.5.0from
schweitzpgi:ch-broadcast.error

Conversation

@schweitzpgi

@schweitzpgi schweitzpgi commented Nov 9, 2023

Copy link
Copy Markdown
Collaborator

This PR fixes the bug #875 in the C++ AST Bridge whereby any additional qubits passed to a call to a quantum operation were treated as a control qubits even if no ctrl template argument was provided.

Old behavior:

  cudaq::qreg qs(2);
  auto& q1 = qs.front();
  auto& q2 = qs.back();
  
  // this applies a controlled operation:
  x<cudaq::ctrl>(q1, q2); // cnot operation with q1 as the control qubit

  // this was also treated as a controlled operation:
  x(q1, q2);  // cnot operation with q1 as the control qubit
  
  // this was treated as broadcast
  x(qs); // applies x to both qubits

New behavior:

  cudaq::qreg qs(2);
  auto& q1 = qs.front();
  auto& q2 = qs.back();
  
  // this still applies a controlled operation:
  x<cudaq::ctrl>(q1, q2); // cnot operation with q1 as the control qubit

  // this is now treated as a broadcast
  x(q1, q2);  // gives a warning that the behavior changed

  // this is still treated as broadcast
  x(qs); // applies x to both qubits

@schweitzpgi schweitzpgi added the bug fix To be listed under Bug Fixes in the release notes label Nov 9, 2023
@schweitzpgi schweitzpgi added this to the release 0.5.0 milestone Nov 9, 2023
@github-actions

github-actions Bot commented Nov 9, 2023

Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions Bot pushed a commit that referenced this pull request Nov 9, 2023
@github-actions

Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions Bot pushed a commit that referenced this pull request Nov 10, 2023
qunatum operations, which were erroneously assumed to be control qubits.
@github-actions

Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions Bot pushed a commit that referenced this pull request Nov 10, 2023

@boschmitt boschmitt left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks Eric. Overall LGTM.

I think we should add more tests to see how the compiler is behaving with a wide variety of combinations. Some that I could think of (likely not comprehensive):

__qpu__ void example() {
  cudaq::qreg q(4);
  cudaq::qreg w(2);
  cudaq::qubit t;
  
  x(q[0], q[1], q[2]); // Broadcast
  x(q); // Broadcast
  x(q, t, w); // Broadcast?
  x<cudaq::ctrl>(q); // Error? Or controls: q[0], q[1], q[2], target: q[3]?
  x<cudaq::ctrl>(q, t); // controls: q[0], q[1], q[2], q[3] target: t
  x<cudaq::ctrl>(t, q); // Error?

  swap(w); // Ok
  swap(q[0], q[1]); // Ok
  swap(q[0], q[1], q[2]); // Error
  swap(q); // Error ? Broadcast (swap(q[0], q[1]) swap(q[2],q[3])?
  swap<cudaq::ctrl>(w); // Error?
  swap<cudaq::ctrl>(q[0], q[1], q[2]); // Error? Or control: q[0], targets: q[1], q[2]
  swap<cudaq::ctrl>(q, w); // controls: q, targets: w[0], w[1]
  swap<cudaq::ctrl>(w, q); // Error?
}

@schweitzpgi

Copy link
Copy Markdown
Collaborator Author

Thanks Eric. Overall LGTM.

I think we should add more tests to see how the compiler is behaving with a wide variety of combinations. Some that I could think of (likely not comprehensive):

__qpu__ void example() {
  cudaq::qreg q(4);
  cudaq::qreg w(2);
  cudaq::qubit t;
  
  x(q[0], q[1], q[2]); // Broadcast
  x(q); // Broadcast
  x(q, t, w); // Broadcast?
  x<cudaq::ctrl>(q); // Error? Or controls: q[0], q[1], q[2], target: q[3]?
  x<cudaq::ctrl>(q, t); // controls: q[0], q[1], q[2], q[3] target: t
  x<cudaq::ctrl>(t, q); // Error?

  swap(w); // Ok
  swap(q[0], q[1]); // Ok
  swap(q[0], q[1], q[2]); // Error
  swap(q); // Error ? Broadcast (swap(q[0], q[1]) swap(q[2],q[3])?
  swap<cudaq::ctrl>(w); // Error?
  swap<cudaq::ctrl>(q[0], q[1], q[2]); // Error? Or control: q[0], targets: q[1], q[2]
  swap<cudaq::ctrl>(q, w); // controls: q, targets: w[0], w[1]
  swap<cudaq::ctrl>(w, q); // Error?
}

Thanks for looking at this one, Bruno.

I agree that those are all good ideas and should be added to the tests.

It may not be completely obvious, but this particular PR is a one-off strictly for the 0.5.0 release branch. I think if we add more tests, they really belong on main via a different PR. So I'll make your suggestions an issue.

@github-actions

Copy link
Copy Markdown

CUDA Quantum Docs Bot: A preview of the documentation can be found here.

github-actions Bot pushed a commit that referenced this pull request Nov 13, 2023
@schweitzpgi

Copy link
Copy Markdown
Collaborator Author

@bettinaheim to merge

@bettinaheim
bettinaheim merged commit a74e3ac into NVIDIA:releases/v0.5.0 Nov 14, 2023
@github-actions github-actions Bot locked and limited conversation to collaborators Nov 14, 2023
@bettinaheim bettinaheim changed the title Introduce a warning and fix for the cuda quantum broadcast form of Fix broadcasting behavior and log a warning for behavior change Nov 15, 2023
@bettinaheim bettinaheim added the breaking change Change breaks backwards compatibility label Nov 15, 2023
@schweitzpgi
schweitzpgi deleted the ch-broadcast.error branch December 9, 2025 16:48
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

breaking change Change breaks backwards compatibility bug fix To be listed under Bug Fixes in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants