def test_x(self): """Single uncontrolled X""" tweedledum_circuit = Circuit() tweedledum_circuit.apply_operator(X(), [tweedledum_circuit.create_qubit()]) circuit = tweedledum2qiskit(tweedledum_circuit) expected = QuantumCircuit(1) expected.x(0) self.assertEqual(circuit, expected)
def test_cx_1_0(self): """CX(1, 0)""" tweedledum_circuit = Circuit() qubits = list() qubits.append(tweedledum_circuit.create_qubit()) qubits.append(tweedledum_circuit.create_qubit()) tweedledum_circuit.apply_operator(X(), [qubits[1], qubits[0]]) circuit = tweedledum2qiskit(tweedledum_circuit) expected = QuantumCircuit(2) expected.append(XGate().control(1, ctrl_state="1"), [1, 0]) self.assertEqual(expected, circuit)
def test_cx_0_1(self): """CX(0, 1)""" tweedledum_circuit = Circuit() qubits = list() qubits.append(tweedledum_circuit.create_qubit()) qubits.append(tweedledum_circuit.create_qubit()) tweedledum_circuit.apply_operator(X(), [qubits[0], qubits[1]]) circuit = tweedledum2qiskit(tweedledum_circuit) expected = QuantumCircuit(2) expected.append(XGate().control(1, ctrl_state='1'), [0, 1]) self.assertEqual(circuit, expected)
def test_cx_qreg(self): """CX(0, 1) with qregs parameter""" tweedledum_circuit = Circuit() qubits = list() qubits.append(tweedledum_circuit.create_qubit()) qubits.append(tweedledum_circuit.create_qubit()) tweedledum_circuit.apply_operator(X(), [qubits[1], qubits[0]]) qr = QuantumRegister(2, "qr") circuit = tweedledum2qiskit(tweedledum_circuit, qregs=[qr]) expected = QuantumCircuit(qr) expected.append(XGate().control(1, ctrl_state="1"), [qr[1], qr[0]]) self.assertEqual(expected, circuit)
def test_wires(): """Adding Cbits and Qubits to a circuit.""" circuit = Circuit() q0 = circuit.create_qubit() c0 = circuit.create_cbit() c1 = circuit.create_cbit() q1 = circuit.create_qubit() assert circuit.num_cbits() == 2 assert circuit.num_qubits() == 2 assert circuit.num_wires() == 4 assert q0.uid() == c0.uid() assert q1.uid() == c1.uid() assert q0.uid() != c1.uid()
def test_wires(self): """Adding Cbits and Qubits to a circuit.""" circuit = Circuit() q0 = circuit.create_qubit() c0 = circuit.create_cbit() c1 = circuit.create_cbit() q1 = circuit.create_qubit() self.assertEqual(circuit.num_cbits(), 2) self.assertEqual(circuit.num_qubits(), 2) self.assertEqual(circuit.num_wires(), 4) self.assertEqual(q0.uid(), c0.uid()) self.assertEqual(q1.uid(), c1.uid()) self.assertNotEqual(q0.uid(), c1.uid())
def qiskit_dag_to_tweedledum(qiskit_dag): circuit = Circuit() qubits = [circuit.create_qubit() for i in range(len(qiskit_dag.qubits))] qubits_map = dict(zip(qiskit_dag.qubits, qubits)) cbits = [circuit.create_cbit() for i in range(len(qiskit_qc.clbits))] cbits_map = dict(zip(qiskit_qc.clbits, cbits)) for node in qiskit_dag.op_nodes(): op, ctrl_state = _convert_qiskit_operator(node.op) qs = [qubits_map.get(qubit) for qubit in node.qargs] cs = [cbits_map.get(cbit) for cbit in node.cargs] for i, polarity in enumerate(ctrl_state): if polarity == '0': qs[i] = ~qs[i] circuit.apply_operator(op, qs, cs) return circuit
def qiskit_qc_to_tweedledum(qiskit_qc): circuit = Circuit() qubits = [circuit.create_qubit() for i in range(len(qiskit_qc.qubits))] qubits_map = dict(zip(qiskit_qc.qubits, qubits)) cbits = [circuit.create_cbit() for i in range(len(qiskit_qc.clbits))] cbits_map = dict(zip(qiskit_qc.clbits, cbits)) for instruction, qargs, cargs in qiskit_qc.data: op, ctrl_state = _convert_qiskit_operator(instruction) qs = [qubits_map.get(qubit) for qubit in qargs] cs = [cbits_map.get(cbit) for cbit in cargs] for i, polarity in enumerate(ctrl_state): if polarity == '0': qs[i] = ~qs[i] circuit.apply_operator(op, qs, cs) return circuit
def phaseflip_circuit(f: BoolFunction, method: str, config: dict() = {}): r"""Synthesizes a Boolean Function as a phaseflip oracle. A phaseflip oracle is a quantum operator `Pf` specified by a Boolean function `f` for which the effect on all computational basis states is given by Bf : |x>|0>^a --> (-1)^{f(x)}|x>|0>^a Note that you can easily construct a phase oracle from a bit oracle by sandwiching the controlled X gate on the result qubit by a X and H gate. """ synthesizer = _METHOD_TO_CALLABLE.get(method) if synthesizer is None: raise ValueError(f"Unrecognized synthesis method: {method}") if method in ["pprm", "pkrm", "spectrum"]: if f.num_outputs() > 1: raise ValueError("TT based methods only work for single output functions") if f.num_inputs() > 16: raise ValueError( "TT based methods only work for functions with at most 16 inputs" ) if f"{method}_synth" not in config: config[f"{method}_synth"] = {"phase_esop": True} else: config[f"{method}_synth"]["phase_esop"] = True return synthesizer(f.truth_table(output_bit=0), config) circuit = Circuit() num_qubits = f.num_inputs() + f.num_outputs() qubits = [circuit.create_qubit() for _ in range(num_qubits)] cbits = list() for i in range(f.num_inputs(), num_qubits): circuit.apply_operator(X(), [qubits[i]]) circuit.apply_operator(H(), [qubits[i]]) synthesizer(circuit, qubits, cbits, f.logic_network(), config) for i in range(f.num_inputs(), num_qubits): circuit.apply_operator(H(), [qubits[i]]) circuit.apply_operator(X(), [qubits[i]]) return circuit
def test_applying_operators(self): """Adding Cbits and Qubits to a circuit.""" circuit = Circuit() q0 = circuit.create_qubit() c0 = circuit.create_cbit() q1 = circuit.create_qubit() circuit.apply_operator(H(), [q1]) circuit.apply_operator(X(), [q1, q0]) circuit.apply_operator(H(), [q1], [c0]) circuit.apply_operator(X(), [q1, q0], [c0])