【发布时间】:2020-11-15 10:34:10
【问题描述】:
我想通过 Python 代码将复杂对象传递给 Q# 操作。我在两边都定义了相同的数据结构:在 Python 中作为 class,在 Q# 中作为 newtype。 然后我在 Python 中准备了复杂对象的 JSON 表示(利用 json 包和 this answer)并尝试将其传递给 Q# 操作,但出现以下错误:
Received invalid parameters. Please fix and try again:
n: Unexpected initial token 'String' when populating object. Expected JSON object or array. Path '', line 1, position 171.
这是 Python 代码
import qsharp
import json
import inspect
from json.test import JsonTest
class ObjectEncoder(json.JSONEncoder):
def default(self, obj):
if hasattr(obj, "to_json"):
return self.default(obj.to_json())
elif hasattr(obj, "__dict__"):
d = dict(
(key, value)
for key, value in inspect.getmembers(obj)
if not key.startswith("__")
and not inspect.isabstract(value)
and not inspect.isbuiltin(value)
and not inspect.isfunction(value)
and not inspect.isgenerator(value)
and not inspect.isgeneratorfunction(value)
and not inspect.ismethod(value)
and not inspect.ismethoddescriptor(value)
and not inspect.isroutine(value)
)
return self.default(d)
return obj
class U:
x = 0
y = 0
z = 0
def __init__(self, x, y, z) :
self.x = x
self.y = y
self.z = z
class N:
t = [U(0,0,0), U(3.14,0,0)]
q = 3
def __init__(self, t, q) :
self.t = t
self.q = q
obj = N([U(3.14, 0, 0), U(0, 3.14, 0)], 3)
jsonObj = json.dumps(obj, cls=ObjectEncoder, indent=2, sort_keys=False)
print(jsonObj)
JsonTest.simulate(n=jsonObj)
这是该代码打印的 JSON 表示
{
"q": 3,
"t": [
{
"x": 3.14,
"y": 0,
"z": 0
},
{
"x": 0,
"y": 3.14,
"z": 0
}
]
}
这是Q#代码
namespace json.test {
open Microsoft.Quantum.Convert;
open Microsoft.Quantum.Intrinsic;
newtype U = (
x : Double,
y : Double,
z : Double
);
newtype N = (
t : U[],
q : Int
);
operation JsonTest(n : N) : Int {
let r = n::q * Length(n::t); // just do sometinhg
Message($"t = {n::t} q = {n::q}");
return r;
}
}
Q# 真的支持复杂对象的 JSON 表示吗?
【问题讨论】:
-
我还尝试以更类似于 Q# 的方式手动编码复杂对象
([(3.1, 0.0, 0.0),(0.0, 3.1, 0.0)], 3)但我总是遇到同样的错误