def test_moments() -> None: circ0 = qf.ghz_circuit(range(5)) dag = qf.DAGCircuit(circ0) circ = dag.moments() assert circ.size() == dag.depth() circ1 = qf.Circuit([ qf.Z(0), qf.Z(1), qf.Z(2), qf.CNot(0, 1), qf.Measure(0, 0), qf.Measure(1, 1), qf.Measure(2, 2), ]) moments = qf.DAGCircuit(circ1).moments() print() print(moments) assert len(moments) == 3 assert len(moments[0]) == 3 # type: ignore assert len(moments[1]) == 1 # type: ignore assert len(moments[2]) == 3 # type: ignore with pytest.warns(DeprecationWarning): _ = dag.layers()
def test_if() -> None: circ = qf.Circuit() c = ["c0", "c1"] circ += qf.Store(c[0], 0) circ += qf.Store(c[1], 0) circ += qf.If(qf.X(0), c[1], value=False) circ += qf.Measure(0, c[0]) ket = circ.run() assert ket.memory[c[0]] == 1 assert circ.evolve().memory[c[0]] == 1 circ = qf.Circuit() circ += qf.Store(c[0], 0) circ += qf.Store(c[1], 0) circ += qf.If(qf.X(0), c[1]) circ += qf.Measure(0, c[0]) ket = circ.run() assert ket.memory[c[0]] == 0 assert circ.evolve().memory[c[0]] == 0 circ = qf.Circuit() circ += qf.Store(c[0], 0) circ += qf.Store(c[1], 0) circ += qf.If(qf.X(0), c[1], value=False) circ += qf.Measure(0, c[0]) ket = circ.run() assert ket.memory[c[0]] == 1 assert circ.evolve().memory[c[0]] == 1
def test_if(): circ = qf.Circuit() c = qf.Register('c') circ += qf.Move(c[0], 0) circ += qf.Move(c[1], 1) circ += qf.If(qf.X(0), c[1]) circ += qf.Measure(0, c[0]) ket = circ.run() assert ket.memory[c[0]] == 1 assert circ.evolve().memory[c[0]] == 1 circ = qf.Circuit() c = qf.Register('c') circ += qf.Move(c[0], 0) circ += qf.Move(c[1], 0) circ += qf.If(qf.X(0), c[1]) circ += qf.Measure(0, c[0]) ket = circ.run() assert ket.memory[c[0]] == 0 assert circ.evolve().memory[c[0]] == 0 circ = qf.Circuit() c = qf.Register('c') circ += qf.Move(c[0], 0) circ += qf.Move(c[1], 0) circ += qf.If(qf.X(0), c[1], value=False) circ += qf.Measure(0, c[0]) ket = circ.run() assert ket.memory[c[0]] == 1 assert circ.evolve().memory[c[0]] == 1
def test_reset_one(): prog = qf.Program() prog += qf.Call('X', params=[], qubits=[0]) prog += qf.Call('X', params=[], qubits=[1]) prog += qf.Reset(0) prog += qf.Measure(0, ('b', 0)) prog += qf.Measure(1, ('b', 1)) ket = prog.run() assert ket.memory[('b', 0)] == 0 assert ket.memory[('b', 1)] == 1
def test_measure(): prog = qf.Program() prog += qf.Measure(0, ('c', 0)) prog += qf.Call('X', params=[], qubits=[0]) prog += qf.Measure(0, ('c', 1)) ket = prog.run() assert ket.qubits == (0,) # assert ket.cbits == [('c', 0), ('c', 1)] # FIXME assert ket.memory[('c', 0)] == 0 assert ket.memory[('c', 1)] == 1
def test_measure() -> None: prog = qf.Circuit() prog += qf.Measure(0, ("c", 0)) prog += qf.X(0) prog += qf.Measure(0, ("c", 1)) ket = prog.run() assert ket.qubits == (0, ) assert ket.memory[("c", 0)] == 0 assert ket.memory[("c", 1)] == 1 assert str(qf.Measure(4)) == "Measure 4" assert str(qf.Measure(4, "c0")) == "Measure 4 c0"
def test_measure(): quil = "MEASURE 1" cmd = qf.parse_quil(quil)[0] assert isinstance(cmd, qf.Measure) assert cmd.quil() == quil quil = "MEASURE 3 reg0[2]" cmd = qf.parse_quil(quil)[0] assert isinstance(cmd, qf.Measure) assert cmd.quil() == quil prog = qf.parse_quil(quil) print(len(prog)) print(prog) _test("MEASURE 0", qf.Measure(0)) _test("MEASURE 0 b[1]", qf.Measure(0, qf.Register('b')[1]))
def test_gates_to_latex(): circ = qf.Circuit() circ += qf.I(7) circ += qf.X(0) circ += qf.Y(1) circ += qf.Z(2) circ += qf.H(3) circ += qf.S(4) circ += qf.T(5) circ += qf.S_H(6) circ += qf.T_H(7) circ += qf.RX(-0.5*pi, 0) circ += qf.RY(0.5*pi, 1) circ += qf.RZ((1/3)*pi, 1) circ += qf.RY(0.222, 1) circ += qf.TX(0.5, 0) circ += qf.TY(0.5, 1) circ += qf.TZ(0.4, 1) circ += qf.TZ(0.47276, 1) # Gate with cunning hack gate = qf.RZ(0.4, 1) gate.params['theta'] = qf.Parameter('\\theta') circ += gate circ += qf.CNOT(1, 2) circ += qf.CNOT(2, 1) circ += qf.CZ(1, 3) circ += qf.SWAP(1, 5) circ += qf.ISWAP(4, 2) # circ += qf.Barrier(0, 1, 2, 3, 4, 5, 6) # Not yet supported circ += qf.CCNOT(1, 2, 3) circ += qf.CSWAP(4, 5, 6) circ += qf.P0(0) circ += qf.P1(1) circ += qf.Reset(2) circ += qf.Reset(4, 5, 6) circ += qf.H(4) # circ += qf.Reset() # FIXME. Should fail with clear error message circ += qf.XX(0.25, 1, 3) circ += qf.YY(0.75, 1, 3) circ += qf.ZZ(1/3, 3, 1) circ += qf.Measure(0) latex = qf.circuit_to_latex(circ) print(latex)
def test_measure_until(): prog = qf.Program() prog += qf.TRUE(('c', 2)) prog += qf.Label('redo') prog += qf.Call('X', [], [0]) prog += qf.Call('H', [], [0]) prog += qf.Measure(0, ('c', 2)) prog += qf.JumpUnless('redo', ('c', 2)) ket = prog.run() assert ket.memory[('c', 2)] == 1
def test_reset(): ro = qf.Register() prog = qf.Program() prog += qf.Move(ro[0], 1) prog += qf.Call('X', params=[], qubits=[0]) prog += qf.Reset() prog += qf.Measure(0, ro[1]) ket = prog.run() assert ket.qubits == (0, ) assert ket.memory[ro[0]] == 1 assert ket.memory[ro[1]] == 0