Skip to content
This repository was archived by the owner on Jan 12, 2024. It is now read-only.
Merged
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
10 changes: 0 additions & 10 deletions .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,11 @@ FROM mcr.microsoft.com/vscode/devcontainers/universal:1-linux

USER root

RUN apt update \
&& apt-get install -y --no-install-recommends \
ninja-build \
clang-11 \
clang-tidy-11 \
build-essential \
&& apt-get clean \
&& rm -rf /var/lib/apt/lists/

# Install CMake 3.20 (required since apt-get uses 3.16 and repo requires 3.20)
RUN curl -SsL https://github.com/Kitware/CMake/releases/download/v3.20.5/cmake-3.20.5-linux-x86_64.sh -o cmakeinstall.sh \
&& echo "f582e02696ceee81818dc3378531804b2213ed41c2a8bc566253d16d894cefab cmakeinstall.sh" | sha256sum -c --strict - \
&& chmod +x cmakeinstall.sh \
&& ./cmakeinstall.sh --prefix=/usr/local --exclude-subdir \
&& rm cmakeinstall.sh


USER codespace
2 changes: 1 addition & 1 deletion global.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"msbuild-sdks": {
"Microsoft.Quantum.Sdk": "0.18.2109163417-beta"
"Microsoft.Quantum.Sdk": "0.22.185393-alpha"
}
}
3 changes: 2 additions & 1 deletion src/Qir/.clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
# https://clang.llvm.org/extra/clang-tidy/checks/list.html

Checks:
'bugprone-*,readability-identifier-*,readability-braces-around-statements,cert*,\
'bugprone-*,-bugprone-easily-swappable-parameters,\
readability-identifier-*,readability-braces-around-statements,cert*,\
-llvmlibc-callee-namespace,-llvmlibc-implementation-in-namespace,\
-llvmlibc-restrict-system-libc-headers,-modernize-use-trailing-return-type,\
-fuchsia-default-arguments-calls,-fuchsia-default-arguments-declarations,
Expand Down
7 changes: 7 additions & 0 deletions src/Qir/Common/cmake/qir_cmake_include.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ set(WARNING_FLAGS "${WARNING_FLAGS} -Weverything")
# -Wpre-c++20-compat.
# -Wpre-c++2b-compat-pedantic (= -Wpre-c++2b-compat).

include(CheckCCompilerFlag)
check_c_compiler_flag(-Wreserved-identifier HAVE_RESERVED_IDENTIFIER_WARNING)
if(HAVE_RESERVED_IDENTIFIER_WARNING)
# We need to be able to use `__` prefix for QIR names like `__quantum__rt__*` and `__quantum__qis__*`.
set(WARNING_FLAGS "${WARNING_FLAGS} -Wno-reserved-identifier")
endif()

# https://clang.llvm.org/docs/DiagnosticsReference.html#wc-98-compat-pedantic
set(WARNING_FLAGS "${WARNING_FLAGS} -Wno-c++98-compat-pedantic")

Expand Down
10 changes: 5 additions & 5 deletions src/Qir/Runtime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ while on macOS, `prerequisites.ps1` relies on the [`brew` package manager](https

#### Windows pre-reqs

1. Install Clang 11, Ninja and CMake from the public distros.
1. Install Clang 13, Ninja and CMake from the public distros.
1. Add all three to your/system `%PATH%`.
1. Install VS 2019 and enable "Desktop development with C++" component (Clang uses MSVC's standard library on Windows).
1. Install clang-tidy and clang-format if your Clang/LLVM packages didn't include the tools.
Expand All @@ -42,11 +42,11 @@ Running cmake from the editors will likely default to MSVC or clang-cl and fail.
1. In the Ubuntu's terminal:
1. `$ sudo apt install cmake` (`$ cmake --version` should return 3.16.3)
1. `$ sudo apt-get install ninja-build` (`$ ninja --version` should return 1.10.0)
1. `$ sudo apt install clang-11` (`$ clang++-11 --version` should return 11.0.0)
1. `$ sudo apt install clang-13` (`$ clang++-13 --version` should return 13.0.0)
1. Set Clang as the preferred C/C++ compiler:
- $ export CC=/usr/bin/clang-11
- $ export CXX=/usr/bin/clang++-11
1. `$ sudo apt install clang-tidy-11` (`$ clang-tidy-11 --version` should return 'LLVM version 11.0.0')
- $ export CC=/usr/bin/clang-13
- $ export CXX=/usr/bin/clang++-13
1. `$ sudo apt install clang-tidy-13` (`$ clang-tidy-13 --version` should return 'LLVM version 13.0.0')
1. Install the same version of dotnet as specified by qsharp-runtime [README](../../../README.md)

See [https://code.visualstudio.com/docs/remote/wsl] on how to use VS Code with WSL.
Expand Down
2 changes: 1 addition & 1 deletion src/Qir/Runtime/lib/QIR/QubitManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -423,7 +423,7 @@ namespace Quantum
{
areaIndex = freeQubitsInAreas[(size_t)areaIndex].prevAreaWithFreeQubits;
id = freeQubitsInAreas[(size_t)areaIndex].FreeQubitsReuseAllowed.TakeQubitFromFront(
sharedQubitStatusArray);
sharedQubitStatusArray);
} while ((areaIndex != 0) && (id == NoneMarker));

// We remember previous area where a free qubit was found or 0 if none were found.
Expand Down
37 changes: 22 additions & 15 deletions src/Qir/Runtime/lib/QIR/arrays.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ QirArray::QirArray(TItemCount countItems, TItemSize itemSizeBytes, TDimCount dim

assert(this->count * (TBufSize)itemSizeInBytes < std::numeric_limits<TBufSize>::max());
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
const TBufSize bufferSize = this->count * itemSizeInBytes;
const TBufSize bufferSize = (TBufSize)this->count * (TBufSize)itemSizeInBytes;
Copy link
Contributor

Choose a reason for hiding this comment

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

(nit) For the future: It is probably more efficient to do the operation first, and then cast the result.

Suggested change
const TBufSize bufferSize = (TBufSize)this->count * (TBufSize)itemSizeInBytes;
const TBufSize bufferSize = (TBufSize)(this->count * itemSizeInBytes);

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think that would work here, yes. But I was a little concerned about a future where these two types would differ. Right now, this->count is of type TItemCount while itemSizeInBytes is of type TItemSize. It just so happens that both of these are uint32_t, but since they are independently defined they could vary. If we want to tie these values together more closely, we could consider consolidating TItemCount and TItemSize into one type.

if (bufferSize > 0)
{
this->buffer = new char[bufferSize];
Expand Down Expand Up @@ -146,7 +146,7 @@ QirArray::QirArray(const QirArray& other)

assert((TBufSize)(this->count) * this->itemSizeInBytes < std::numeric_limits<TBufSize>::max());
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
const TBufSize size = this->count * this->itemSizeInBytes;
const TBufSize size = (TBufSize)this->count * (TBufSize)this->itemSizeInBytes;
if (this->count > 0)
{
this->buffer = new char[size];
Expand All @@ -167,7 +167,7 @@ QirArray::~QirArray()
char* QirArray::GetItemPointer(TItemCount index) const
{
assert(index < this->count);
return &this->buffer[index * this->itemSizeInBytes];
return &this->buffer[static_cast<TBufSize>(index * this->itemSizeInBytes)];
}

void QirArray::Append(const QirArray* other)
Expand All @@ -178,7 +178,7 @@ void QirArray::Append(const QirArray* other)

assert((TBufSize)(other->count) * other->itemSizeInBytes < std::numeric_limits<TBufSize>::max());
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
const TBufSize otherSize = other->count * other->itemSizeInBytes;
const TBufSize otherSize = (TBufSize)other->count * (TBufSize)other->itemSizeInBytes;

if (otherSize == 0)
{
Expand All @@ -187,7 +187,7 @@ void QirArray::Append(const QirArray* other)

assert((TBufSize)(this->count) * this->itemSizeInBytes < std::numeric_limits<TBufSize>::max());
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
const TBufSize thisSize = this->count * this->itemSizeInBytes;
const TBufSize thisSize = (TBufSize)this->count * (TBufSize)this->itemSizeInBytes;

char* newBuffer = new char[thisSize + otherSize];
if (thisSize)
Expand Down Expand Up @@ -585,14 +585,16 @@ extern "C"
assert((QirArray::TBufSize)rangeRunCount * itemSizeInBytes <
std::numeric_limits<QirArray::TBufSize>::max());
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
const QirArray::TBufSize rangeChunkSize = rangeRunCount * itemSizeInBytes;
const QirArray::TBufSize rangeChunkSize =
(QirArray::TBufSize)rangeRunCount * (QirArray::TBufSize)itemSizeInBytes;

QirArray::TItemCount dst = 0;
QirArray::TItemCount src = (QirArray::TItemCount)(singleIndexRunCount * range.start);
while (src < array->count)
{
assert(dst < slice->count);
memcpy(&slice->buffer[dst * itemSizeInBytes], &array->buffer[src * itemSizeInBytes], rangeChunkSize);
memcpy(&slice->buffer[static_cast<QirArray::TBufSize>(dst * itemSizeInBytes)],
&array->buffer[static_cast<QirArray::TBufSize>(src * itemSizeInBytes)], rangeChunkSize);
src += rowCount;
dst += rangeRunCount;
}
Expand All @@ -603,22 +605,25 @@ extern "C"
assert((QirArray::TBufSize)singleIndexRunCount * itemSizeInBytes <
std::numeric_limits<QirArray::TBufSize>::max());
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.
const QirArray::TBufSize chunkSize = singleIndexRunCount * itemSizeInBytes;
QirArray::TItemCount dst = 0;
QirArray::TItemCount src = (QirArray::TItemCount)(singleIndexRunCount * range.start);
const QirArray::TBufSize chunkSize =
(QirArray::TBufSize)singleIndexRunCount * (QirArray::TBufSize)itemSizeInBytes;
QirArray::TItemCount dst = 0;
QirArray::TItemCount src = (QirArray::TItemCount)(singleIndexRunCount * range.start);
while (src < array->count)
{
assert(dst < slice->count);

int64_t srcInner = src; // The `srcInner` can go negative in the end of the last iteration.
for (int64_t index = range.start; index != range.end; index += range.step)
{
assert((dst * itemSizeInBytes + chunkSize) <= (slice->count * slice->itemSizeInBytes));
assert(((QirArray::TItemSize)dst * itemSizeInBytes + (QirArray::TItemSize)chunkSize) <=
(QirArray::TItemSize)slice->count * slice->itemSizeInBytes);
assert((srcInner * (int64_t)itemSizeInBytes + (int64_t)chunkSize) <=
(array->count * array->itemSizeInBytes));
((int64_t)array->count * (int64_t)array->itemSizeInBytes));
assert(srcInner >= 0);

memcpy(&slice->buffer[dst * itemSizeInBytes], &array->buffer[srcInner * itemSizeInBytes], chunkSize);
memcpy(&slice->buffer[static_cast<QirArray::TBufSize>(dst * itemSizeInBytes)],
&array->buffer[static_cast<QirArray::TBufSize>(srcInner * itemSizeInBytes)], chunkSize);
srcInner += (singleIndexRunCount * range.step);
dst += singleIndexRunCount;
}
Expand Down Expand Up @@ -660,14 +665,16 @@ extern "C"
std::numeric_limits<QirArray::TBufSize>::max());
// Using `<` rather than `<=` to calm down the compiler on 32-bit arch.

const QirArray::TBufSize chunkSize = singleIndexRunCount * itemSizeInBytes;
const QirArray::TBufSize chunkSize =
(QirArray::TBufSize)singleIndexRunCount * (QirArray::TBufSize)itemSizeInBytes;

QirArray::TItemCount dst = 0;
QirArray::TItemCount src = (QirArray::TItemCount)(singleIndexRunCount * index);
while (src < array->count)
{
assert(dst < project->count);
memcpy(&project->buffer[dst * itemSizeInBytes], &array->buffer[src * itemSizeInBytes], chunkSize);
memcpy(&project->buffer[static_cast<QirArray::TBufSize>(dst * itemSizeInBytes)],
&array->buffer[static_cast<QirArray::TBufSize>(src * itemSizeInBytes)], chunkSize);
src += rowCount;
dst += singleIndexRunCount;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Qir/Runtime/lib/QIR/callables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ QirTupleHeader* FlattenControlArrays(QirTupleHeader* tuple, int depth)
// Copy the controls into the new array. This array doesn't own the qubits so must use the generic constructor.
QirArray* combinedControls = new QirArray(cControls, qubitSize);
char* dst = combinedControls->buffer;
[[maybe_unused]] const char* dstEnd = dst + qubitSize * cControls;
[[maybe_unused]] const char* dstEnd = dst + static_cast<size_t>(qubitSize * cControls);
current = outer;
QirTupleHeader* last = nullptr;
for (int i = 0; i < depth; i++)
Expand All @@ -383,7 +383,7 @@ QirTupleHeader* FlattenControlArrays(QirTupleHeader* tuple, int depth)

QirArray* controls = current->controls;

const QirArray::TBufSize blockSize = qubitSize * controls->count;
const QirArray::TBufSize blockSize = (QirArray::TBufSize)qubitSize * (QirArray::TBufSize)controls->count;
// Make sure we don't overflow `TBufSize` on 32-bit arch:
assert((blockSize >= qubitSize) && (blockSize >= controls->count));

Expand Down
17 changes: 10 additions & 7 deletions src/Qir/Runtime/lib/Simulators/FullstateSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,15 @@ namespace Quantum
void DumpState()
{
std::cout << "*********************" << std::endl;
this->GetState([](size_t idx, double re, double im) {
if (!Close(re, 0.0) || !Close(im, 0.0))
this->GetState(
[](size_t idx, double re, double im)
{
std::cout << "|" << std::bitset<8>(idx) << ">: " << re << "+" << im << "i" << std::endl;
}
return true;
});
if (!Close(re, 0.0) || !Close(im, 0.0))
{
std::cout << "|" << std::bitset<8>(idx) << ">: " << re << "+" << im << "i" << std::endl;
}
return true;
});
std::cout << "*********************" << std::endl;
}

Expand Down Expand Up @@ -541,7 +543,8 @@ namespace Quantum

private:
TDumpToLocationCallback const dumpToLocationCallback = [](size_t idx, double re, double im,
TDumpLocation location) -> bool {
TDumpLocation location) -> bool
{
std::ostream& outStream = *reinterpret_cast<std::ostream*>(location);

if (!Close(re, 0.0) || !Close(im, 0.0))
Expand Down
15 changes: 12 additions & 3 deletions src/Qir/Runtime/prerequisites.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ if ($Env:ENABLE_QIRRUNTIME -ne "false") {
if (!(Get-Command clang -ErrorAction SilentlyContinue) -or `
!(Get-Command clang-format -ErrorAction SilentlyContinue) -or `
(Test-Path Env:/AGENT_OS)) {
choco install llvm --version=11.1.0 --allow-downgrade
choco install llvm --version=13.0.0 --allow-downgrade
Write-Host "##vso[task.setvariable variable=PATH;]$($env:SystemDrive)\Program Files\LLVM\bin;$Env:PATH"
}
if (!(Get-Command ninja -ErrorAction SilentlyContinue)) {
Expand All @@ -28,12 +28,21 @@ if ($Env:ENABLE_QIRRUNTIME -ne "false") {
brew install clang-format
}
} else {
$needClang = !(Get-Command clang-13 -ErrorAction SilentlyContinue)
if (Get-Command sudo -ErrorAction SilentlyContinue) {
if ($needClang) {
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -
sudo add-apt-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-13 main"
}
sudo apt update
sudo apt-get install -y ninja-build clang-11 clang-tidy-11 clang-format-11
sudo apt-get install -y ninja-build clang-13 clang-tidy-13 clang-format-13
} else {
if ($needClang) {
wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|apt-key add -
add-apt-repository "deb http://apt.llvm.org/focal/ llvm-toolchain-focal-13 main"
}
apt update
apt-get install -y ninja-build clang-11 clang-tidy-11 clang-format-11
apt-get install -y ninja-build clang-13 clang-tidy-13 clang-format-13
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/Qir/Runtime/unittests/QirRuntimeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ TEST_CASE("Arrays: one dimensional", "[qir_support]")

TEST_CASE("Arrays: multiple dimensions", "[qir_support]")
{
const size_t count = 5 * 3 * 4; // 60
const size_t count = (size_t)(5 * 3 * 4); // 60
QirArray* a = __quantum__rt__array_create(sizeof(int), 3, (int64_t)5, (int64_t)3, (int64_t)4);
REQUIRE(__quantum__rt__array_get_dim(a) == 3);
REQUIRE(__quantum__rt__array_get_size(a, 0) == 5);
Expand Down
66 changes: 35 additions & 31 deletions src/Qir/Tests/FullstateSimulator/FullstateSimulatorTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -346,45 +346,49 @@ TEST_CASE("Fullstate simulator: get qubit state of Bell state", "[fullstate_simu
iqa->ControlledX(1, &qs[0], qs[1]);
// 1/sqrt(2)(|00> + |11>)x|0>

dynamic_cast<IDiagnostics*>(sim.get())->GetState([](size_t idx, double re, double im) {
norm += re * re + im * im;
REQUIRE(idx < 4);
switch (idx)
dynamic_cast<IDiagnostics*>(sim.get())->GetState(
[](size_t idx, double re, double im)
{
case 0:
case 3:
REQUIRE((1 / sqrt(2.0) == Approx(re).epsilon(0.0001)));
REQUIRE(im == 0.0);
break;
default:
REQUIRE(re == 0.0);
REQUIRE(im == 0.0);
break;
}
return idx < 3; // the last qubit is in separable |0> state
});
norm += re * re + im * im;
REQUIRE(idx < 4);
switch (idx)
{
case 0:
case 3:
REQUIRE((1 / sqrt(2.0) == Approx(re).epsilon(0.0001)));
REQUIRE(im == 0.0);
break;
default:
REQUIRE(re == 0.0);
REQUIRE(im == 0.0);
break;
}
return idx < 3; // the last qubit is in separable |0> state
});
REQUIRE(1.0 == Approx(norm).epsilon(0.0001));
norm = 0.0;

iqa->Y(qs[2]);
// 1/sqrt(2)(|00> + |11>)xi|1>

dynamic_cast<IDiagnostics*>(sim.get())->GetState([](size_t idx, double re, double im) {
norm += re * re + im * im;
switch (idx)
dynamic_cast<IDiagnostics*>(sim.get())->GetState(
[](size_t idx, double re, double im)
{
case 4:
case 7:
REQUIRE(re == 0.0);
REQUIRE(1 / sqrt(2.0) == Approx(im).epsilon(0.0001));
break;
default:
REQUIRE(re == 0.0);
REQUIRE(im == 0.0);
break;
}
return true; // get full state
});
norm += re * re + im * im;
switch (idx)
{
case 4:
case 7:
REQUIRE(re == 0.0);
REQUIRE(1 / sqrt(2.0) == Approx(im).epsilon(0.0001));
break;
default:
REQUIRE(re == 0.0);
REQUIRE(im == 0.0);
break;
}
return true; // get full state
});
REQUIRE(1.0 == Approx(norm).epsilon(0.0001));
norm = 0.0;

Expand Down
2 changes: 1 addition & 1 deletion src/Qir/Tools/Microsoft.Quantum.Qir.Runtime.Tools.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@

<ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging" Version="5.0.0" />
<PackageReference Include="Microsoft.Quantum.QirGeneration" Version="0.18.2109163417-beta" />
<PackageReference Include="Microsoft.Quantum.QirGeneration" Version="0.22.185393-alpha" />
</ItemGroup>

<ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/Qir/check-sources-formatted.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ if (-not $IsMacOS) { # We do not control the clang-format version on MacOS, an

$clangFormatCommand = "clang-format"
if(($IsLinux) -or ((Test-Path Env:/AGENT_OS) -and ($Env:AGENT_OS.StartsWith("Lin")))) {
$script:clangFormatCommand = "clang-format-11"
$script:clangFormatCommand = "clang-format-13"
}

$OldErrorActionPreference = $ErrorActionPreference
Expand Down
Loading