def test_pyquil_to_circuit(): prog = pyquil.Program(QUILPROG) circ = qf.forest.pyquil_to_circuit(prog) prog_new = qf.forest.circuit_to_pyquil(circ) print(prog_new) assert str(prog_new) == QUILPROG
def test_pyquil_to_circuit_more(): # Check will ignore or convert DECLARE, MEASURE prog = pyquil.Program(BELL_STATE_MEASURE) circ = qf.forest.pyquil_to_circuit(prog) prog_new = qf.forest.circuit_to_pyquil(circ) print(circ.qubits) print(prog_new)
def test_null_complier(): quil = "XOR c[0] c[1]" prog0 = pyquil.Program(quil) nc = qf.forest.NullCompiler() nc.get_version_info() prog1 = nc.quil_to_native_quil(prog0) assert prog1 == prog0 prog2 = nc.native_quil_to_executable(prog0) assert prog2 == prog0
def test_exceptions(): circ = qf.Circuit() circ += qf.T_H(0) with pytest.raises(ValueError): qf.forest.circuit_to_pyquil(circ) quil = "XOR c[0] c[1]" prog = pyquil.Program(quil) with pytest.raises(ValueError): # Not protoquil circ = qf.forest.pyquil_to_circuit(prog) qvm = qf.forest.QuantumFlowQVM() qvm.load(BELL_STATE_MEASURE) with pytest.raises(NotImplementedError): qvm.write_memory(region_name='ro') with pytest.raises(NotImplementedError): qvm.run().wait() qvm.read_from_memory_region(region_name='ro', offsets=[1])
def test_wavefunction(): wf_program = pyquil.Program(pyquil.Declare('ro', 'BIT', 1), pyquil.H(0), pyquil.CNOT(0, 1), pyquil.MEASURE(0, ('ro', 0)), pyquil.H(0)) wf_expected1 = np.array([0. + 0.j, 0. + 0.j, 0.70710678 + 0.j, -0.70710678 + 0.j]) wf_expected0 = np.array([0.70710678 + 0.j, 0.70710678 + 0.j, 0. + 0.j, 0. + 0.j, ]) qvm = qf.forest.QuantumFlowQVM() qvm.load(wf_program).run().wait() wf = qvm.wavefunction() res = qvm.read_from_memory_region(region_name='ro')[0] for _ in range(TRIALS): if res == 0: assert np.all(np.isclose(wf.amplitudes, wf_expected0)) else: assert np.all(np.isclose(wf.amplitudes, wf_expected1))
def test_pyquil_to_latex(): prog = pyquil.Program(BELL_STATE_MEASURE) circ = qf.forest.pyquil_to_circuit(prog) latex = qf.circuit_to_latex(circ) img = qf.render_latex(latex) assert img is not None
assert QUILPROG == str(prog) def test_pyquil_to_circuit(): prog = pyquil.Program(QUILPROG) circ = qf.forest.pyquil_to_circuit(prog) prog_new = qf.forest.circuit_to_pyquil(circ) print(prog_new) assert str(prog_new) == QUILPROG TRIALS = 100 BELL_STATE = pyquil.Program(pyquil.H(0), pyquil.CNOT(0, 1)) BELL_STATE_MEASURE = """DECLARE ro BIT[2] H 0 CNOT 0 1 MEASURE 0 ro[0] MEASURE 1 ro[1] PRAGMA not_a_pragma HALT """ def test_pyquil_to_circuit_more(): # Check will ignore or convert DECLARE, MEASURE prog = pyquil.Program(BELL_STATE_MEASURE) circ = qf.forest.pyquil_to_circuit(prog)