Skip to content
Closed
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
7 changes: 4 additions & 3 deletions pytket/extensions/qiskit/qiskit_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -788,17 +788,17 @@ def _get_qiskit_control_state(bool_list: list[bool]) -> str:

def _param_to_tk(p: float | ParameterExpression) -> sympy.Expr:
if isinstance(p, ParameterExpression):
return sympy.sympify(str(p)) / sympy.pi
return p.sympify() / sympy.pi
return p / sympy.pi


def _param_to_qiskit(
p: sympy.Expr, symb_map: dict[Parameter, sympy.Symbol]
) -> float | ParameterExpression | Parameter:
ppi = p * sympy.pi
ppi = sympify(p) * sympy.pi
if len(ppi.free_symbols) == 0:
return float(ppi.evalf())
return Parameter(str(sympify(ppi)))
return Parameter(sympify(ppi))


def _get_params(
Expand Down Expand Up @@ -1192,6 +1192,7 @@ def tk_to_qiskit(
name_spl = p.name.split("_UUID_", 2)
if len(name_spl) == 2: # noqa: PLR2004
p_name, uuid_str = name_spl
print(uuid_str)
uuid = UUID(uuid_str)
# See Parameter.__init__() in qiskit/circuit/parameter.py.
new_p = Parameter(p_name, uuid=uuid)
Expand Down
44 changes: 44 additions & 0 deletions tests/qiskit_convert_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import json
import os
import warnings
from collections import Counter
Expand Down Expand Up @@ -823,6 +824,49 @@ def test_convert_multi_c_reg() -> None:
assert circ.get_commands()[1].args == [Bit("tk_SCRATCH_BIT", 0), q1]


def test_convert_symbolic_circ() -> None:
with open(
"sympy_qiskit_issue1.json"
) as f:
circ = Circuit.from_dict(json.load(f))
for g in circ:
print(g) # noqa: T201

from pytket.extensions.qiskit.qiskit_convert import ( # noqa: PLC0415
qiskit_to_tk,
tk_to_qiskit,
)

qc = tk_to_qiskit(circ)

print(qc) # noqa: T201

tc = qiskit_to_tk(qc)

print(tc) # noqa: T201


def test_convert_symbolic_circ_2() -> None:
with open(
"sympy_qiskit_issue2.json"
) as f:
circ = Circuit.from_dict(json.load(f))
for g in circ:
print(g) # noqa: T201

from pytket.extensions.qiskit.qiskit_convert import ( # noqa: PLC0415
qiskit_to_tk,
tk_to_qiskit,
)

qc = tk_to_qiskit(circ)

print(qc) # noqa: T201

tc = qiskit_to_tk(qc)

print(tc) # noqa: T201

# test that tk_to_qiskit works after adding OpType.CRx and OpType.CRy
def test_crx_and_cry() -> None:
tk_circ = Circuit(2)
Expand Down
Loading