Skip to content

Commit

Permalink
* Addressing review comments:
Browse files Browse the repository at this point in the history
  -- No braces for single statements in if/else/for as per LLVM
standards.
  -- Additional test for remote execution of Python code
  -- Check to emit error if custom operation is encountered in QIR
  • Loading branch information
khalatepradnya committed Jul 12, 2024
1 parent 961beb9 commit b7389a5
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 14 deletions.
6 changes: 3 additions & 3 deletions lib/Frontend/nvqpp/ASTBridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,11 +234,11 @@ class QPUCodeFinder : public clang::RecursiveASTVisitor<QPUCodeFinder> {
cudaq::details::getTagNameOfFunctionDecl(func, mangler);
runChecks = true;
}
if (cudaq::ASTBridgeAction::ASTBridgeConsumer::isQuantum(func)) {
if (cudaq::ASTBridgeAction::ASTBridgeConsumer::isQuantum(func))
runChecks = true;
} else {
else
quantumTypesNotAllowed = true;
}

if (runChecks) {
quantumTypesNotAllowed = false;
// Run semantics checks on the kernel class.
Expand Down
3 changes: 2 additions & 1 deletion lib/Optimizer/CodeGen/ConvertToQIRProfile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -547,7 +547,8 @@ struct VerifyQIRProfilePass
func.walk([&](Operation *op) {
if (auto call = dyn_cast<LLVM::CallOp>(op)) {
auto funcName = call.getCalleeAttr().getValue();
if (!funcName.startswith("__quantum_")) {
if (!funcName.startswith("__quantum_") ||
funcName.equals(cudaq::opt::QIRCustomOp)) {
call.emitOpError("unexpected call in QIR base profile");
passFailed = true;
return WalkResult::advance();
Expand Down
3 changes: 1 addition & 2 deletions lib/Optimizer/Transforms/GetConcreteMatrix.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,8 @@ class CustomUnitaryPattern

auto parentModule = customOp->getParentOfType<ModuleOp>();
auto funcOp = parentModule.lookupSymbol<func::FuncOp>(generator);
if (!funcOp) {
if (!funcOp)
return failure();
}

// The generator function returns a concrete matrix. If prior passes have
// run to constant fold and lift array values, the generator function will
Expand Down
18 changes: 18 additions & 0 deletions python/tests/remote/test_remote_code_exec.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import subprocess
import time
import psutil
import numpy as np

import cudaq
from cudaq import spin
Expand Down Expand Up @@ -351,6 +352,23 @@ def test_complex_vqe_named_lambda_sweep_grad(gradient):
test_complex_vqe_named_lambda(cudaq.optimizers.Adam(), gradient)


def test_arbitrary_unitary_synthesis():
cudaq.register_operation("custom_h",
1. / np.sqrt(2.) * np.array([1, 1, 1, -1]))
cudaq.register_operation("custom_x", np.array([0, 1, 1, 0]))

@cudaq.kernel
def bell():
qubits = cudaq.qvector(2)
custom_h(qubits[0])
custom_x.ctrl(qubits[0], qubits[1])

counts = cudaq.sample(bell)
assert len(counts) == 2
assert "00" in counts
assert "11" in counts


# leave for gdb debugging
if __name__ == "__main__":
loc = os.path.abspath(__file__)
Expand Down
13 changes: 5 additions & 8 deletions runtime/nvqir/NVQIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -597,20 +597,17 @@ void __quantum__qis__custom_unitary__adj(std::complex<double> *unitary,
std::vector<std::vector<std::complex<double>>> unitaryConj2D;
for (std::size_t r = 0; r < nToPowTwo; r++) {
std::vector<std::complex<double>> row;
for (std::size_t c = 0; c < nToPowTwo; c++) {
for (std::size_t c = 0; c < nToPowTwo; c++)
row.push_back(std::conj(unitary[r * nToPowTwo + c]));
}
unitaryConj2D.push_back(row);
}
for (std::size_t r = 0; r < nToPowTwo; r++) {
for (std::size_t c = 0; c < r; c++) {
for (std::size_t r = 0; r < nToPowTwo; r++)
for (std::size_t c = 0; c < r; c++)
std::swap(unitaryConj2D[r][c], unitaryConj2D[c][r]);
}
}
std::vector<std::complex<double>> unitaryFlattened;
for (auto const &row : unitaryConj2D) {
for (auto const &row : unitaryConj2D)
unitaryFlattened.insert(unitaryFlattened.end(), row.begin(), row.end());
}

nvqir::getCircuitSimulatorInternal()->applyCustomOperation(unitaryFlattened,
ctrlsVec, tgtsVec);
}
Expand Down

0 comments on commit b7389a5

Please sign in to comment.