@@ -53,23 +53,25 @@ option(XACC_BUILD_IONTRAP "Build the iontrap plugin" OFF)
53
53
option (PY_ENV "Create and use a Conda Python environment" OFF )
54
54
55
55
if (PY_ENV)
56
- # Set the environment name to xacc
57
- set (ANACONDA_ENV_NAME "xacc" )
58
-
59
- # List of required Python packages as separate arguments
60
- set (PYTHON_PACKAGES "iPOPO" "qsrs" "rpcq" "qiskit" "qsearch" "scipy" "myqlm" "scikit-quant" "qfactor" "pybind11" )
61
-
62
56
# Check if the environment already exists by using conda info --envs
63
57
execute_process (
64
- COMMAND conda info --envs
65
- OUTPUT_VARIABLE CONDA_ENVS
58
+ COMMAND conda info --base
59
+ OUTPUT_VARIABLE CONDA_BASE_PATH
66
60
OUTPUT_STRIP_TRAILING_WHITESPACE
67
61
)
62
+ # Set the environment name to xacc
63
+ set (ANACONDA_ENV_NAME "xacc" )
64
+ # Set the base path to the Conda installation
65
+ message (STATUS "Using Conda: ${CONDA_BASE_PATH} " )
66
+ set (ANACONDA_ENV_PATH "${CONDA_BASE_PATH} /envs/${ANACONDA_ENV_NAME} " )
67
+ # List of required Python packages as separate arguments
68
+ set (PYTHON_PACKAGES "iPOPO" "qsrs" "rpcq" "qiskit" "qsearch" "scipy" "myqlm" "scikit-quant" "qfactor" )
68
69
69
- # Check if the xacc environment is found in the list of conda environments
70
- string (REGEX MATCH "[^ \n ]*${ANACONDA_ENV_NAME} [^ \n ]*" ANACONDA_ENV_PATH "${CONDA_ENVS} " )
71
70
72
- if (NOT ANACONDA_ENV_PATH)
71
+ # Check if the xacc environment is found in the list of conda environments
72
+ if (EXISTS "${ANACONDA_ENV_PATH} " )
73
+ message (STATUS "Conda environment ${ANACONDA_ENV_NAME} already exists at ${ANACONDA_ENV_PATH} " )
74
+ else ()
73
75
# If the environment does not exist, create it
74
76
message (STATUS "Conda environment ${ANACONDA_ENV_NAME} not found. Creating the environment." )
75
77
execute_process (COMMAND conda create -y --name ${ANACONDA_ENV_NAME} python==3.12.4
@@ -81,17 +83,20 @@ if(PY_ENV)
81
83
82
84
# After creation, update the environment path
83
85
execute_process (
84
- COMMAND conda info --envs
85
- OUTPUT_VARIABLE CONDA_ENVS
86
+ COMMAND conda info --base
87
+ OUTPUT_VARIABLE CONDA_BASE_PATH
86
88
OUTPUT_STRIP_TRAILING_WHITESPACE
87
89
)
90
+ set (ANACONDA_ENV_PATH "${CONDA_BASE_PATH} /envs/${ANACONDA_ENV_NAME} " )
88
91
89
- string (REGEX MATCH "[^ \n ]*${ANACONDA_ENV_NAME} [^ \n ]*" ANACONDA_ENV_PATH "${CONDA_ENVS} " )
90
92
message (STATUS "Conda environment ${ANACONDA_ENV_NAME} created at ${ANACONDA_ENV_PATH} " )
91
- else ()
92
- message (STATUS "Conda environment ${ANACONDA_ENV_NAME} already exists at ${ANACONDA_ENV_PATH} " )
93
93
endif ()
94
94
95
+ # Set the path to Python executable directly from the conda environment
96
+ set (Python_EXECUTABLE "${ANACONDA_ENV_PATH} /bin/python" )
97
+
98
+ message (STATUS "Using Python from Conda environment: ${Python_EXECUTABLE}${colorreset} " )
99
+
95
100
# Install necessary Python packages using conda run to ensure environment context
96
101
foreach (package ${PYTHON_PACKAGES} )
97
102
message (STATUS "Checking installation of Python package: ${package} " )
@@ -108,7 +113,7 @@ if(PY_ENV)
108
113
else ()
109
114
message (STATUS "Installing Python package: ${package} " )
110
115
execute_process (
111
- COMMAND conda run -n ${ANACONDA_ENV_NAME} ${Python_EXECUTABLE} pip install ${package}
116
+ COMMAND conda run -n ${ANACONDA_ENV_NAME} ${Python_EXECUTABLE} -m pip install ${package}
112
117
113
118
#COMMAND conda run -n ${ANACONDA_ENV_NAME} ${Python_EXECUTABLE} -m pip install ${package}
114
119
RESULT_VARIABLE PIP_INSTALL_RESULT
@@ -117,28 +122,13 @@ if(PY_ENV)
117
122
if (NOT PIP_INSTALL_RESULT EQUAL "0" )
118
123
message (WARNING "Failed to install Python package ${package} . Continuing without it." )
119
124
endif ()
125
+
120
126
endif ()
121
127
endforeach ()
122
-
123
- execute_process (
124
- COMMAND conda activate ${ANACONDA_ENV_NAME}
125
- )
126
-
127
- # Set the path to Python executable directly from the conda environment
128
- set (Python_EXECUTABLE "${ANACONDA_ENV_PATH} /bin/python" )
129
-
130
- message (STATUS "Using Python from Conda environment: ${Python_EXECUTABLE} {colorreset}" )
131
128
else ()
132
- message (STATUS "PY_ENV is OFF, not using Conda environment. " )
129
+ message (STATUS "Using system Python " )
133
130
endif ()
134
131
135
-
136
-
137
-
138
-
139
-
140
-
141
-
142
132
if (XACC_BUILD_ANNEALING)
143
133
set (CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DANNEALING_ENABLED" )
144
134
else ()
0 commit comments