def test_body_without_drawing_order_and_gates_not_parallel(): drawer = _drawer.CircuitDrawer() eng = MainEngine(drawer, []) old_tolatex = _drawer.to_latex _drawer.to_latex = lambda x, drawing_order, draw_gates_in_parallel: x qubit1 = eng.allocate_qubit() qubit2 = eng.allocate_qubit() qubit3 = eng.allocate_qubit() H | qubit1 H | qubit2 H | qubit3 CNOT | (qubit1, qubit3) del qubit1 eng.flush() circuit_lines = drawer.get_latex() _drawer.to_latex = old_tolatex settings = _to_latex.get_default_settings() settings['gates']['AllocateQubitGate']['draw_id'] = True code = _to_latex._body(circuit_lines, settings, draw_gates_in_parallel=False) # line1_gate1 is after the cnot line2_gate_4 idx1 = code.find("node[xstyle] (line2_gate4)") idx2 = code.find("node[none] (line1_gate1)") assert idx1 < idx2
def test_default_settings(): settings = _to_latex.get_default_settings() assert isinstance(settings, dict) assert 'gate_shadow' in settings assert 'lines' in settings assert 'gates' in settings assert 'control' in settings
def test_large_gates(): drawer = _drawer.CircuitDrawer() eng = MainEngine(drawer, []) old_tolatex = _drawer.to_latex _drawer.to_latex = lambda x: x qubit1 = eng.allocate_qubit() qubit2 = eng.allocate_qubit() qubit3 = eng.allocate_qubit() class MyLargeGate(BasicGate): def __str__(self): return "large_gate" H | qubit2 MyLargeGate() | (qubit1, qubit3) H | qubit2 eng.flush() circuit_lines = drawer.get_latex() _drawer.to_latex = old_tolatex settings = _to_latex.get_default_settings() settings['gates']['AllocateQubitGate']['draw_id'] = True code = _to_latex._body(circuit_lines, settings) assert code.count("large_gate") == 1 # 1 large gate was applied # check that large gate draws lines, also for qubits it does not act upon assert code.count("edge[") == 5 assert code.count("{H};") == 2
def test_qubit_allocations_at_zero(): drawer = _drawer.CircuitDrawer() eng = MainEngine(drawer, []) old_tolatex = _drawer.to_latex _drawer.to_latex = lambda x: x a = eng.allocate_qureg(4) CNOT | (a[0], a[2]) CNOT | (a[0], a[3]) CNOT | (a[0], a[2]) CNOT | (a[1], a[3]) del a eng.flush() circuit_lines = drawer.get_latex() _drawer.to_latex = old_tolatex settings = _to_latex.get_default_settings() settings['gates']['AllocateQubitGate']['allocate_at_zero'] = True code = _to_latex._body(copy.deepcopy(circuit_lines), settings) assert code.count("gate0) at (0") == 4 settings['gates']['AllocateQubitGate']['allocate_at_zero'] = False code = _to_latex._body(copy.deepcopy(circuit_lines), settings) assert code.count("gate0) at (0") == 3 del settings['gates']['AllocateQubitGate']['allocate_at_zero'] code = _to_latex._body(copy.deepcopy(circuit_lines), settings) assert code.count("gate0) at (0") == 3
def test_body_with_drawing_order_and_gates_not_parallel(): drawer = _drawer.CircuitDrawer() eng = MainEngine(drawer, []) old_tolatex = _drawer.to_latex _drawer.to_latex = lambda x, drawing_order, draw_gates_in_parallel: x qubit1 = eng.allocate_qubit() qubit2 = eng.allocate_qubit() qubit3 = eng.allocate_qubit() H | qubit1 H | qubit2 H | qubit3 CNOT | (qubit1, qubit3) # replicates the above order: first the 3 allocations, then the 3 Hadamard and 1 CNOT gates order = [0, 1, 2, 0, 1, 2, 0] del qubit1 eng.flush() circuit_lines = drawer.get_latex() _drawer.to_latex = old_tolatex settings = _to_latex.get_default_settings() settings['gates']['AllocateQubitGate']['draw_id'] = True code = _to_latex._body(circuit_lines, settings, drawing_order=order, draw_gates_in_parallel=False) # and the CNOT is at position 4.0, because of the offsets # which are 0.5 * 3 * 2 (due to three Hadamards) + the initialisations assert code.count("node[phase] (line0_gate4) at (4.0,-0)") == 1 assert code.count("node[xstyle] (line2_gate4) at (4.0,-2)") == 1
def test_body(): drawer = _drawer.CircuitDrawer() eng = MainEngine(drawer, []) old_tolatex = _drawer.to_latex _drawer.to_latex = lambda x : x qubit1 = eng.allocate_qubit() qubit2 = eng.allocate_qubit() H | qubit1 H | qubit2 CNOT | (qubit1, qubit2) X | qubit2 Measure | qubit2 CNOT | (qubit2, qubit1) Z | qubit2 del qubit1 eng.flush() circuit_lines = drawer.get_latex() _drawer.to_latex = old_tolatex settings = _to_latex.get_default_settings() settings['gates']['AllocateQubitGate']['draw_id']=True code = _to_latex._body(circuit_lines, settings) assert code.count("{{{}}}".format(str(H))) == 2 # 2 hadamard gates assert code.count("{$\Ket{0}") == 2 # two qubits allocated assert code.count("xstyle") == 3 # 1 cnot, 1 not gate assert code.count("measure") == 1 # 1 measurement assert code.count("{{{}}}".format(str(Z))) == 1 # 1 Z gate assert code.count("{red}") == 2
def test_body_with_drawing_order_and_gates_parallel(): drawer = _drawer.CircuitDrawer() eng = MainEngine(drawer, []) old_tolatex = _drawer.to_latex _drawer.to_latex = lambda x, drawing_order, draw_gates_in_parallel: x qubit1 = eng.allocate_qubit() qubit2 = eng.allocate_qubit() qubit3 = eng.allocate_qubit() H | qubit1 H | qubit2 H | qubit3 CNOT | (qubit1, qubit3) # replicates the above order order = [ 0, 1, 2, # initializations 0, 1, 2, # H1, H3, H2 0 # CNOT ] del qubit1 eng.flush() circuit_lines = drawer.get_latex() _drawer.to_latex = old_tolatex settings = _to_latex.get_default_settings() settings['gates']['AllocateQubitGate']['draw_id'] = True code = _to_latex._body(circuit_lines, settings, drawing_order=order, draw_gates_in_parallel=True) # there are three Hadamards in parallel assert code.count("node[pos=.5] {H}") == 3 # line1_gate0 is initialisation # line1_gate1 is empty # line1_gate2 is for Hadamard on line1 # line1_gate3 is empty # XOR of CNOT is node[xstyle] (line1_gate4) assert code.count("node[xstyle] (line2_gate4)") == 1 # and the CNOT is at position 1.4, because of the offsets assert code.count("node[phase] (line0_gate4) at (1.4") == 1 assert code.count("node[xstyle] (line2_gate4) at (1.4") == 1
def test_body(): drawer = _drawer.CircuitDrawer() eng = MainEngine(drawer, []) old_tolatex = _drawer.to_latex _drawer.to_latex = lambda x: x qubit1 = eng.allocate_qubit() qubit2 = eng.allocate_qubit() qubit3 = eng.allocate_qubit() H | qubit1 H | qubit2 CNOT | (qubit1, qubit2) X | qubit2 Measure | qubit2 CNOT | (qubit2, qubit1) Z | qubit2 C(Z) | (qubit1, qubit2) C(Swap) | (qubit1, qubit2, qubit3) SqrtX | qubit1 SqrtSwap | (qubit1, qubit2) get_inverse(SqrtX) | qubit1 C(SqrtSwap) | (qubit1, qubit2, qubit3) get_inverse(SqrtSwap) | (qubit1, qubit2) C(Swap) | (qubit3, qubit1, qubit2) C(SqrtSwap) | (qubit3, qubit1, qubit2) del qubit1 eng.flush() circuit_lines = drawer.get_latex() _drawer.to_latex = old_tolatex settings = _to_latex.get_default_settings() settings['gates']['AllocateQubitGate']['draw_id'] = True code = _to_latex._body(circuit_lines, settings) # swap draws 2 nodes + 2 lines each, so is sqrtswap gate, csqrtswap, # inv(sqrt_swap), and cswap. assert code.count("swapstyle") == 36 # CZ is two phases plus 2 from CNOTs + 2 from cswap + 2 from csqrtswap assert code.count("phase") == 8 assert code.count("{{{}}}".format(str(H))) == 2 # 2 hadamard gates assert code.count("{$\Ket{0}") == 3 # 3 qubits allocated # 1 cnot, 1 not gate, 3 SqrtSwap, 1 inv(SqrtSwap) assert code.count("xstyle") == 7 assert code.count("measure") == 1 # 1 measurement assert code.count("{{{}}}".format(str(Z))) == 1 # 1 Z gate assert code.count("{red}") == 3
def test_invalid_number_of_qubits(gate, n_qubits): drawer = _drawer.CircuitDrawer() eng = MainEngine(drawer, []) old_tolatex = _drawer.to_latex _drawer.to_latex = lambda x, drawing_order, draw_gates_in_parallel: x qureg = eng.allocate_qureg(n_qubits) gate | (*qureg,) eng.flush() circuit_lines = drawer.get_latex() _drawer.to_latex = old_tolatex settings = _to_latex.get_default_settings() settings['gates']['AllocateQubitGate']['draw_id'] = True with pytest.raises(RuntimeError): _to_latex._body(circuit_lines, settings)
def test_qubit_lines_classicalvsquantum1(): drawer = _drawer.CircuitDrawer() eng = MainEngine(drawer, []) old_tolatex = _drawer.to_latex _drawer.to_latex = lambda x: x qubit1 = eng.allocate_qubit() H | qubit1 Measure | qubit1 X | qubit1 circuit_lines = drawer.get_latex() _drawer.to_latex = old_tolatex settings = _to_latex.get_default_settings() code = _to_latex._body(circuit_lines, settings) assert code.count("edge[") == 4
def drawer_settings(): xmon_settings = get_default_settings() xmon_settings['gate_shadow'] = False xmon_settings['gates']['ExpWGate'] = { 'height': 0.8, 'offset': 0.3, 'pre_offset': 0.2, 'width': 1.75 } xmon_settings['gates']['ExpZGate'] = { 'height': 0.8, 'offset': 0.3, 'pre_offset': 0.2, 'width': 1. } xmon_settings['gates']['Exp11Gate'] = { 'height': .8, 'offset': 0.3, 'pre_offset': 0.3, 'width': 1. } return xmon_settings