def test_decomposition_operation_compiled(self): """Test decomposition operation gets decomposed if compiled""" # create a test program sf_prog = Program(1) with sf_prog.context as q: ops.Pgate(0.43) | q[0] xir_prog = io.to_xir(sf_prog) expected = [("Pgate", [0.43], (0,))] assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected xir_prog = io.to_xir(sf_prog.compile(compiler="gaussian")) assert xir_prog.statements[0].name == "Sgate" assert xir_prog.statements[1].name == "Rgate"
def test_empty_program(self): """Test that an empty program is correctly converted""" # create a test program sf_prog = Program(4, name="") xir_prog = io.to_xir(sf_prog) assert xir_prog.serialize() == "" assert xir_prog.version == "0.1.0"
def test_program_with_options(self): """Test that program with options is correctly converted""" # create a test program sf_prog = Program(4, name="test_program") sf_prog.run_options = {"shots": 2} sf_prog.backend_options = {"cutoff_dim": 5} xir_prog = io.to_xir(sf_prog) assert xir_prog.options == {"_name_": "test_program", "cutoff_dim": 5, "shots": 2}
def test_two_mode_gate(self): """Test two mode gate converts""" sf_prog = Program(4) with sf_prog.context as q: ops.BSgate(0.54, -0.324) | (q[3], q[0]) xir_prog = io.to_xir(sf_prog) expected = [("BSgate", [0.54, -0.324], (3, 0))] assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected
def test_free_par_str(self): """Test a FreeParameter with some transformations converts properly""" sf_prog = Program(2) r, alpha = sf_prog.params("r", "alpha") with sf_prog.context as q: ops.Sgate(r) | q[0] ops.Zgate(3 * pf.log(-alpha)) | q[1] xir_prog = io.to_xir(sf_prog) expected = [("Sgate", ["r", 0.0], (0,)), ("Zgate", ["3*log(-alpha)"], (1,))] assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected
def test_measure_arg_postselect(self): """Test measurement with argument and postselection converts""" # create a test program sf_prog = Program(1) with sf_prog.context as q: ops.MeasureHomodyne(0.43, select=0.543) | q[0] xir_prog = io.to_xir(sf_prog) expected = [("MeasureHomodyne", {"phi": 0.43, "select": 0.543}, (0,))] assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected # repeat with kwargs only sf_prog = Program(1) with sf_prog.context as q: ops.MeasureHomodyne(phi=0.43, select=0.543) | q[0] xir_prog = io.to_xir(sf_prog) assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected
def test_measure_arg(self): """Test measurement with argument converts""" # create a test program sf_prog = Program(1) with sf_prog.context as q: ops.MeasureHomodyne(0.43) | q[0] xir_prog = io.to_xir(sf_prog) expected = [("MeasureHomodyne", {"phi": 0.43}, (0,))] assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected
def test_measure_darkcounts(self): """Test measurement with dark counts""" # create a test program sf_prog = Program(1) with sf_prog.context as q: ops.MeasureFock(dark_counts=2) | q[0] xir_prog = io.to_xir(sf_prog) expected = [("MeasureFock", {"dark_counts": [2]}, (0,))] assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected
def test_measure_postselect(self): """Test measurement with postselection""" # create a test program sf_prog = Program(1) with sf_prog.context as q: ops.MeasureFock(select=2) | q[0] xir_prog = io.to_xir(sf_prog) expected = [("MeasureFock", {"select": [2]}, (0,))] assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected
def test_gate_arg(self): """Test gate with argument converts""" # create a test program sf_prog = Program(2) with sf_prog.context as q: ops.Sgate(0.54, 0.324) | q[1] xir_prog = io.to_xir(sf_prog) expected = [("Sgate", [0.54, 0.324], (1,))] assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected
def test_gate_noarg(self): """Test gate with no argument converts""" # create a test program sf_prog = Program(1) with sf_prog.context as q: ops.Vac | q[0] xir_prog = io.to_xir(sf_prog) expected = [("Vacuum", [], (0,))] assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected
def test_measure_noarg(self): """Test measurement with no argument converts""" # create a test program sf_prog = Program(1) with sf_prog.context as q: ops.MeasureFock() | q[0] xir_prog = io.to_xir(sf_prog) # note that measurement parameters are always dicts expected = [("MeasureFock", {}, (0,))] assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected
def test_decomposition_operation_not_compiled(self): """Test decomposition operation""" # create a test program sf_prog = Program(4) with sf_prog.context as q: ops.Interferometer(U) | q xir_prog = io.to_xir(sf_prog) assert xir_prog.statements[0].name == "Interferometer" param = np.array(xir_prog.statements[0].params) assert np.allclose(param[0], U) assert xir_prog.statements[0].wires == (0, 1, 2, 3)
def test_gate_kwarg(self): """Test gate with keyword argument converts""" # create a test program sf_prog = Program(2) with sf_prog.context as q: ops.Dgate(r=np.abs(0.54 + 0.324j), phi=np.angle(0.54 + 0.324j)) | q[0] xir_prog = io.to_xir(sf_prog) # Note: due to how SF stores quantum commands with the Parameter class, # all kwargs get converted to positional args internally. expected = [("Dgate", [np.abs(0.54 + 0.324j), np.angle(0.54 + 0.324j)], (0,))] assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected
def test_tdm_program(self): """Test the TDM programs converts properly""" sf_prog = TDMProgram(2) with sf_prog.context([1, 2], [3, 4], [5, 6]) as (p, q): ops.Sgate(0.7, 0) | q[1] ops.BSgate(p[0]) | (q[0], q[1]) ops.Rgate(p[1]) | q[1] ops.MeasureHomodyne(p[2]) | q[0] xir_prog = io.to_xir(sf_prog) expected = [ ("Sgate", [0.7, 0], (1,)), ("BSgate", ["p0", 0.0], (0, 1)), ("Rgate", ["p1"], (1,)), ("MeasureHomodyne", {"phi": "p2"}, (0,)), ] assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected
def test_gate_arg_add_declarations(self): """Test gate with argument converts with declarations""" # create a test program sf_prog = Program(2) with sf_prog.context as q: ops.Sgate(0.54, 0.324) | q[1] ops.MeasureFock() | q xir_prog = io.to_xir(sf_prog, add_decl=True) expected = [("Sgate", [0.54, 0.324], (1,)), ("MeasureFock", {}, (0, 1))] assert [(stmt.name, stmt.params, stmt.wires) for stmt in xir_prog.statements] == expected assert len(xir_prog.declarations["gate"]) == 1 assert xir_prog.declarations["gate"][0].wires == (0,) assert xir_prog.declarations["gate"][0].params == ["p0", "p1"] assert len(xir_prog.declarations["out"]) == 1 assert xir_prog.declarations["out"][0].wires == (0, 1) assert xir_prog.declarations["out"][0].params == []