Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Surface Code #7

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
Open

Conversation

justinlietz
Copy link
Collaborator

This PR adds the cudaq::qec::surface_code namespace which includes the surface_code type and the stabilizer_grid helper class. The stabilizer_grid generates the 2d layout of the stabilizers, and which data qubits each stabilizer has support on. Lastly it generates the stabilizers and observables as vectors of cudaq::spin_op which the qec::code expects.

Copy link

copy-pr-bot bot commented Nov 21, 2024

This pull request requires additional validation before any workflows can run on NVIDIA's runners.

Pull request vetters can view their responsibilities here.

Contributors can view more details about this message here.

@boschmitt
Copy link
Collaborator

/ok to test

Copy link
Collaborator

@bmhowe23 bmhowe23 left a comment

Choose a reason for hiding this comment

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

Thanks, Justin. I have a few comments/questions below.

libs/qec/include/cudaq/qec/codes/surface_code.h Outdated Show resolved Hide resolved

/// @brief enumerates the role of a grid site in the surface codes stabilizer
/// grid
enum surface_role { amx, amz, empty };
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just curious: is this "a measure x" and "a measure z", or something else?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

That is a fine interpretation, although in my head I was thinking "ancilla for mx/z".

libs/qec/include/cudaq/qec/codes/surface_code.h Outdated Show resolved Hide resolved
libs/qec/include/cudaq/qec/codes/surface_code.h Outdated Show resolved Hide resolved
Comment on lines +233 to +219
stabilizer(patch p, const std::vector<std::size_t> &x_stabilizers,
const std::vector<std::size_t> &z_stabilizers);
Copy link
Collaborator

Choose a reason for hiding this comment

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

I didn't think we supported pass-by-reference going into a __qpu__ function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

What is the expected behavior of passing a ref in? This seems to work here as well as in the Steane code.

Copy link
Collaborator

Choose a reason for hiding this comment

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

In this case, either pass by value or pass by ref get you the same thing - a subtlety of the ASTBridge MLIR generation. No matter what the signature is, if you pass a std::vector it is modeled as a cc.stdvec which lowers to a span-like type (a pointer and a size). You are always passing by reference no matter what. Eric can elaborate more if needed.

cudaqx::tensor<uint8_t> Lz = surf_code->get_observables_z();

{
// This is just a regression check, this has not been hand tested
Copy link
Collaborator

Choose a reason for hiding this comment

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

What exactly does this comment mean? Where did the data come from?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The data is only generated from itself, just scraped from the terminal and put into a vector here. Other instances of checking against hardcoded bit strings were hand checked to be correct. This one was not, as there are not printed instances of surface code PCMs I could find.

This comment is to make us aware that these are not magic numbers that we must always match. Various permutations would all be valid PCMs of the surface code.

EXPECT_EQ(t.at({i, j}), parity.at({i, j}));
}
{
// This is just a regression check, this has not been hand tested
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same question here.

EXPECT_EQ(t.at({i, j}), parity_x.at({i, j}));
}
{
// This is just a regression check, this has not been hand tested
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same question here.

Copy link
Collaborator

@bmhowe23 bmhowe23 left a comment

Choose a reason for hiding this comment

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

Notes from our meeting today.


/// @brief Generates and keeps track of the 2d grid of stabilizers in the
/// rotated surface code
// Grid layout is arranged from left to right, top to bottom (row major storage)
Copy link
Collaborator

Choose a reason for hiding this comment

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

We need to double check that the docs are rendering as desired for // vs ///.

Comment on lines 45 to 47
// The grid length of 4 corresponds to a distance 3 surface code, which results
// in: e(0,0) e(0,1) Z(0,2) e(0,3) X(1,0) Z(1,1) X(1,2) e(1,3) e(2,0)
// X(2,1) Z(2,2) X(2,3) e(3,0) Z(3,1) e(3,2) e(3,3)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Double-check formatting.

//
// The data qubits are located at the four corners of each of the weight-4
// stabilizers. They are also organized with index increasing from left to
// right, top to bottom: d0 d1 d2 d3 d4 d5 d6 d7 d8
Copy link
Collaborator

Choose a reason for hiding this comment

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

Double check formatting (two spaces vs single spac)

bool operator<(const vec2d &lhs, const vec2d &rhs);

/// @brief Generates and keeps track of the 2d grid of stabilizers in the
/// rotated surface code
Copy link
Collaborator

Choose a reason for hiding this comment

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

Perhaps consider adding link to a reference paper who's convention you're following w.r.t. the grid.

@@ -0,0 +1,396 @@
/****************************************************************-*- C++ -*-****
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit:

Suggested change
/****************************************************************-*- C++ -*-****
/*******************************************************************************

@@ -0,0 +1,87 @@
/****************************************************************-*- C++ -*-****
Copy link
Collaborator

Choose a reason for hiding this comment

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

Nit:

Suggested change
/****************************************************************-*- C++ -*-****
/*******************************************************************************

@bmhowe23
Copy link
Collaborator

/ok to test

@bmhowe23
Copy link
Collaborator

/ok to test

@bmhowe23
Copy link
Collaborator

I don't know if you're seeing this in your local builds or not, but the CI is reporting some potentially relevant warnings:
https://github.com/NVIDIA/cudaqx/actions/runs/12943122467/job/36166508865?pr=7#step:10:232

E.g., this is repeated many times:

In file included from /__w/cudaqx/cudaqx/libs/qec/unittests/test_qec.cpp:14:
/__w/cudaqx/cudaqx/libs/qec/include/cudaq/qec/codes/surface_code.h:164:23: warning: ‘annotate’ attribute directive ignored [-Wattributes]
  164 | __qpu__ void x(patch p);

@justinlietz
Copy link
Collaborator Author

I don't know if you're seeing this in your local builds or not, but the CI is reporting some potentially relevant warnings: https://github.com/NVIDIA/cudaqx/actions/runs/12943122467/job/36166508865?pr=7#step:10:232

E.g., this is repeated many times:

In file included from /__w/cudaqx/cudaqx/libs/qec/unittests/test_qec.cpp:14:
/__w/cudaqx/cudaqx/libs/qec/include/cudaq/qec/codes/surface_code.h:164:23: warning: ‘annotate’ attribute directive ignored [-Wattributes]
  164 | __qpu__ void x(patch p);

I do not see this locally, but I build with -DCMAKE_CXX_FLAGS="-Wno-attributes". Turning this flag off, I do now see this warning, but I'm not sure why surface_code is any different than steane in this regard.

@bmhowe23
Copy link
Collaborator

I don't know if you're seeing this in your local builds or not, but the CI is reporting some potentially relevant warnings: https://github.com/NVIDIA/cudaqx/actions/runs/12943122467/job/36166508865?pr=7#step:10:232
E.g., this is repeated many times:

In file included from /__w/cudaqx/cudaqx/libs/qec/unittests/test_qec.cpp:14:
/__w/cudaqx/cudaqx/libs/qec/include/cudaq/qec/codes/surface_code.h:164:23: warning: ‘annotate’ attribute directive ignored [-Wattributes]
  164 | __qpu__ void x(patch p);

I do not see this locally, but I build with -DCMAKE_CXX_FLAGS="-Wno-attributes". Turning this flag off, I do now see this warning, but I'm not sure why surface_code is any different than steane in this regard.

It turns out this warning is only coming from the test_qec.cpp file, not our actual libraries. Our libs are built with -Wno-attributes, but our tests are not. test_qec.cpp does not #include the Steane code, so the tests are not currently reporting this warning. The prior QEC tests just used the pure QEC interfaces. I think we should add add_compile_options(-Wno-attributes) to libs/qec/unittests/CMakeLists.txt (similar to how we already do that in libs/qec/lib/CMakeLists.txt).

@justinlietz
Copy link
Collaborator Author

/ok to test

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants