def test_pass_cx_cancellation_own_template(self): """ Check the cancellation of CX gates for the apply of a self made template cx-cx. """ qr = QuantumRegister(2, 'qr') circuit_in = QuantumCircuit(qr) circuit_in.h(qr[0]) circuit_in.h(qr[0]) circuit_in.cx(qr[0], qr[1]) circuit_in.cx(qr[0], qr[1]) circuit_in.cx(qr[0], qr[1]) circuit_in.cx(qr[0], qr[1]) circuit_in.cx(qr[1], qr[0]) circuit_in.cx(qr[1], qr[0]) dag_in = circuit_to_dag(circuit_in) qrt = QuantumRegister(2, 'qrc') qct = QuantumCircuit(qrt) qct.cx(0, 1) qct.cx(0, 1) template_list = [qct] pass_ = TemplateOptimization(template_list) dag_opt = pass_.run(dag_in) circuit_expected = QuantumCircuit(qr) circuit_expected.h(qr[0]) circuit_expected.h(qr[0]) dag_expected = circuit_to_dag(circuit_expected) self.assertEqual(dag_opt, dag_expected)
def test_pass_cx_cancellation_template_from_library(self): """ Check the cancellation of CX gates for the apply of the library template cx-cx (2a_2). """ qr = QuantumRegister(2, 'qr') circuit_in = QuantumCircuit(qr) circuit_in.h(qr[0]) circuit_in.h(qr[0]) circuit_in.cx(qr[0], qr[1]) circuit_in.cx(qr[0], qr[1]) circuit_in.cx(qr[0], qr[1]) circuit_in.cx(qr[0], qr[1]) circuit_in.cx(qr[1], qr[0]) circuit_in.cx(qr[1], qr[0]) dag_in = circuit_to_dag(circuit_in) template_list = [template_nct_2a_2()] pass_ = TemplateOptimization(template_list) dag_opt = pass_.run(dag_in) circuit_expected = QuantumCircuit(qr) circuit_expected.h(qr[0]) circuit_expected.h(qr[0]) dag_expected = circuit_to_dag(circuit_expected) self.assertEqual(dag_opt, dag_expected)
def test_pass_template_nct_5a(self): """ Verify the result of template matching and substitution with the template 5a_3. q_0: ───────■─────────■────■── ┌─┴─┐ ┌─┴─┐ │ q_1: ──■──┤ X ├──■──┤ X ├──┼── ┌─┴─┐└───┘┌─┴─┐└───┘┌─┴─┐ q_2: ┤ X ├─────┤ X ├─────┤ X ├ └───┘ └───┘ └───┘ The circuit before optimization is: ┌───┐ ┌───┐ qr_0: ┤ X ├───────────────┤ X ├───── └─┬─┘ ┌───┐┌───┐└─┬─┘ qr_1: ──┼────■──┤ X ├┤ Z ├──┼────■── │ │ └─┬─┘└───┘ │ │ qr_2: ──┼────┼────■────■────■────┼── │ │ ┌───┐┌─┴─┐ │ │ qr_3: ──■────┼──┤ H ├┤ X ├──■────┼── │ ┌─┴─┐└───┘└───┘ ┌─┴─┐ qr_4: ──■──┤ X ├───────────────┤ X ├ └───┘ └───┘ The match is given by [0,1][1,2][2,7], after substitution the circuit becomes: ┌───┐ ┌───┐ qr_0: ┤ X ├───────────────┤ X ├ └─┬─┘ ┌───┐┌───┐└─┬─┘ qr_1: ──┼───────┤ X ├┤ Z ├──┼── │ └─┬─┘└───┘ │ qr_2: ──┼────■────■────■────■── │ │ ┌───┐┌─┴─┐ │ qr_3: ──■────┼──┤ H ├┤ X ├──■── │ ┌─┴─┐└───┘└───┘ qr_4: ──■──┤ X ├─────────────── └───┘ """ qr = QuantumRegister(5, 'qr') circuit_in = QuantumCircuit(qr) circuit_in.ccx(qr[3], qr[4], qr[0]) circuit_in.cx(qr[1], qr[4]) circuit_in.cx(qr[2], qr[1]) circuit_in.h(qr[3]) circuit_in.z(qr[1]) circuit_in.cx(qr[2], qr[3]) circuit_in.ccx(qr[2], qr[3], qr[0]) circuit_in.cx(qr[1], qr[4]) dag_in = circuit_to_dag(circuit_in) template_list = [template_nct_5a_3()] pass_ = TemplateOptimization(template_list) dag_opt = pass_.run(dag_in) circuit_expected = QuantumCircuit(qr) circuit_expected.ccx(qr[3], qr[4], qr[0]) circuit_expected.cx(qr[2], qr[4]) circuit_expected.cx(qr[2], qr[1]) circuit_expected.z(qr[1]) circuit_expected.h(qr[3]) circuit_expected.cx(qr[2], qr[3]) circuit_expected.ccx(qr[2], qr[3], qr[0]) dag_expected = circuit_to_dag(circuit_expected) self.assertEqual(dag_opt, dag_expected)