Skip to content

Commit 619c2f4

Browse files
authored
Migrate to use venv (#685)
* Migrate to use venv * Fix install from source * Add Powershell to prerequisites in README * Remove pytest-regressions from requirements * Change virtual environment activation command to PowerShell Updated instructions for activating the virtual environment from bash to PowerShell.
1 parent 660a19a commit 619c2f4

14 files changed

Lines changed: 254 additions & 347 deletions

.ado/ci.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ jobs:
6767
6868
- script: |
6969
cd $(Build.SourcesDirectory)/azure-quantum
70-
pip install .[all]
70+
pip install .[qiskit,cirq,qsharp,dev]
7171
pytest --cov-report term --cov=azure.quantum --junitxml test-output-azure-quantum.xml $(Build.SourcesDirectory)/azure-quantum
7272
displayName: Run azure-quantum unit tests
7373

.ado/publish.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ extends:
117117
118118
- script: |
119119
cd $(Build.SourcesDirectory)/azure-quantum
120-
pip install .[all]
120+
pip install .[qiskit,cirq,qsharp,dev]
121121
pytest --cov-report term --cov=azure.quantum --junitxml test-output-azure-quantum.xml $(Build.SourcesDirectory)/azure-quantum
122122
displayName: Run Unit-tests
123123

azure-quantum/MANIFEST.in

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ include *.py
33
include *.md
44
recursive-include azure *.py
55
exclude MANIFEST.in
6-
exclude environment.yml
76
recursive-exclude azure *.typed
87
recursive-exclude eng *
98
recursive-exclude tests *

azure-quantum/README.md

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,48 @@ result = job.get_results()
7979

8080
You can find example Python scripts that use the Azure Quantum Python API in the [examples](https://github.com/microsoft/qdk-python/tree/main/azure-quantum/examples) directory.
8181

82+
## Development Setup ##
83+
84+
For developers who want to contribute to this package or run tests locally, follow these steps:
85+
86+
### Prerequisites
87+
88+
- Python 3.9 or later
89+
- Git
90+
- Powershell
91+
92+
### Setting up the development environment
93+
94+
1. Clone the repository:
95+
```bash
96+
git clone https://github.com/microsoft/qdk-python.git
97+
cd qdk-python/azure-quantum
98+
```
99+
100+
2. Set up a virtual environment and install development dependencies:
101+
```powershell
102+
# On Windows (PowerShell)
103+
.\eng\Setup-Dev-Env.ps1
104+
```
105+
106+
3. Run the tests:
107+
```bash
108+
pytest tests/unit/
109+
```
110+
111+
4. (Optional) Install additional provider dependencies:
112+
```bash
113+
# For specific providers
114+
pip install -e .[pulser,quil]
115+
116+
# For all providers (requires Rust toolchain for PyQuil)
117+
pip install -e .[all]
118+
```
119+
120+
### Running Tests
121+
122+
The development environment includes pytest for running unit tests. See [tests/README.md](tests/README.md) for detailed testing instructions.
123+
82124
## Contributing ##
83125

84126
For details on contributing to this package, see the [contributing guide](https://github.com/microsoft/qdk-python/blob/main/CONTRIBUTING.md).

azure-quantum/eng/Record-Tests.ps1

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,14 @@ try
2020
{
2121
Push-Location (Join-Path $PSScriptRoot "../tests/")
2222

23-
conda activate azurequantum
23+
# Activate virtual environment if it exists
24+
$VenvPath = "../venv"
25+
if (Test-Path $VenvPath) {
26+
Write-Host "Activating virtual environment..."
27+
& "$VenvPath\Scripts\Activate.ps1"
28+
} else {
29+
Write-Warning "Virtual environment not found at $VenvPath. Please run Setup-Dev-Env.ps1 first."
30+
}
2431

2532
if ([string]::IsNullOrEmpty($TestFilter)) {
2633
pytest

azure-quantum/eng/Setup-Dev-Env.ps1

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,30 @@ try
77
{
88
Push-Location (Join-Path $PSScriptRoot "../")
99

10-
conda env create -f environment.yml
11-
conda env update -f environment.yml --prune
12-
conda activate azurequantum
10+
# Create virtual environment if it doesn't exist
11+
$VenvPath = "venv"
12+
if (-not (Test-Path $VenvPath)) {
13+
Write-Host "Creating virtual environment..."
14+
python -m venv $VenvPath
15+
}
1316

14-
pip install -e .[qiskit,cirq]
17+
# Activate virtual environment
18+
Write-Host "Activating virtual environment..."
19+
& ".\$VenvPath\Scripts\Activate.ps1"
20+
21+
# Upgrade pip and install build tools
22+
Write-Host "Upgrading pip and installing build tools..."
23+
python -m pip install --upgrade pip setuptools wheel
24+
25+
# Install package in editable mode with optional dependencies
26+
Write-Host "Installing package in editable mode..."
27+
pip install -e .[qiskit,cirq,qsharp,dev]
28+
29+
Write-Host ""
30+
Write-Host "Development environment setup complete!"
31+
Write-Host "To install additional optional dependencies later, run:"
32+
Write-Host " pip install -e .[pulser,quil] # for specific providers"
33+
Write-Host " pip install -e .[all] # for all providers (requires Rust toolchain)"
1534
}
1635
finally
1736
{

azure-quantum/environment.yml

Lines changed: 0 additions & 11 deletions
This file was deleted.

azure-quantum/requirements-dev.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
pytest>=7.1.2
12
vcrpy>=4.3.1 # fixes https://github.com/kevin1024/vcrpy/issues/688
23
azure-devtools>=1.2.0,<2.0
3-
graphviz>=0.20.1
4+
graphviz>=0.20.1

azure-quantum/tests.live/Install-Artifacts.ps1

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,27 +3,25 @@
33

44
<#
55
.SYNOPSIS
6-
Install-Artifacts: set up a Python environment using Anaconda
6+
Install-Artifacts: set up a Python environment using virtual environments
77
#>
88

99
$PackageDir = Split-Path -parent $PSScriptRoot;
1010
$PackageName = $PackageDir | Split-Path -Leaf;
1111
$RootDir = Split-Path -parent $PackageDir;
12-
Import-Module (Join-Path $RootDir "build" "conda-utils.psm1");
13-
Import-Module (Join-Path $RootDir "build" "package-utils.psm1");
12+
Import-Module (Join-Path $RootDir "build" "venv-utils.psm1") -Force;
13+
Import-Module (Join-Path $RootDir "build" "package-utils.psm1") -Force;
1414

15-
# Enable conda hook
16-
Enable-Conda
17-
18-
NewCondaEnvForPackage -PackageName $PackageName
15+
New-VenvForPackage -PackageName $PackageName
1916

2017
if (-not $Env:PYTHON_OUTDIR) {
2118
"" | Write-Host
2219
"== Environment variable $Env:PYTHON_OUTDIR is not set. " | Write-Host
2320
"== We will install $PackageName from source." | Write-Host
2421
"" | Write-Host
2522

26-
Install-PackageInEnv -PackageName "$PackageName[all]" -FromSource $True
23+
$PackageWithExtras = "$PackageName[qiskit,cirq,qsharp,dev]"
24+
Install-PackageInEnv -PackageName $PackageWithExtras -FromSource $True
2725

2826
"" | Write-Host
2927
"== $PackageName installed from source. ==" | Write-Host
@@ -34,7 +32,8 @@ if (-not $Env:PYTHON_OUTDIR) {
3432
"== To use build artifacts, download the artifacts locally and point the variable to this folder." | Write-Warning
3533
"" | Write-Warning
3634
Exit 1
37-
}
35+
}
36+
3837
# this condition is used by the E2E Live test pipeline
3938
elseif ($Env:PICK_QDK_VERSION -eq "auto") {
4039
"== Installing latest published $PackageName package from PyPI..." | Write-Host
@@ -43,9 +42,9 @@ elseif ($Env:PICK_QDK_VERSION -eq "auto") {
4342
"== Preparing environment to use artifacts with version '$Env:PYTHON_VERSION' " | Write-Host
4443
"== from '$Env:PYTHON_OUTDIR'" | Write-Host
4544
if ($Env:PYTHON_VERSION) {
46-
$NameAndVersion = "$PackageName[all]==$($Env:PYTHON_VERSION)"
45+
$NameAndVersion = "$PackageName[qiskit,cirq,qsharp,dev]==$($Env:PYTHON_VERSION)"
4746
} else {
48-
$NameAndVersion = "$PackageName[all]"
47+
$NameAndVersion = "$PackageName[qiskit,cirq,qsharp,dev]"
4948
}
5049

5150
Install-PackageInEnv -PackageName $NameAndVersion -FromSource $False -BuildArtifactPath $Env:PYTHON_OUTDIR

azure-quantum/tests.live/Run.ps1

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Get-ChildItem env:AZURE*, env:*VERSION, env:*OUTDIR | ForEach-Object {
1818
$PackageDir = Split-Path -parent $PSScriptRoot;
1919
$PackageName = $PackageDir | Split-Path -Leaf;
2020
$RootDir = Split-Path -parent $PackageDir;
21-
Import-Module (Join-Path $RootDir "build" "conda-utils.psm1");
21+
Import-Module (Join-Path $RootDir "build" "venv-utils.psm1");
2222
Import-Module (Join-Path $RootDir "build" "package-utils.psm1");
2323

2424
if ($True -eq $SkipInstall) {
@@ -27,24 +27,6 @@ if ($True -eq $SkipInstall) {
2727
& (Join-Path $PSScriptRoot Install-Artifacts.ps1)
2828
}
2929

30-
Enable-Conda
31-
32-
# Try activating the azurequantum conda environment
33-
if ([string]::IsNullOrEmpty($PackageName) -or ($PackageName -eq "azure-quantum")) {
34-
try {
35-
$EnvExists = conda env list | Select-String -Pattern "azurequantum " | Measure-Object | Select-Object -Exp Count
36-
if ($EnvExists) {
37-
conda activate azurequantum
38-
}
39-
}
40-
catch {
41-
Write-Host "##[warning]Failed to active conda environment."
42-
}
43-
}
44-
45-
$EnvName = GetEnvName -PackageName $PackageName
46-
Use-CondaEnv $EnvName
47-
4830
function PyTestMarkExpr() {
4931
param (
5032
[string[]] $AzureQuantumCapabilities

0 commit comments

Comments
 (0)